No way to plot correctly dashed and dotted lines using painters
Show older comments
I know this is an old issue, and believe me, I've been trying all found "solutions" all the week long. None has really worked. Here's an examplary code of 4 responses for underdamped systems used in Matlab 2019a:
K = 2;
w01 = 1;
w02 = 2;
Zeta1 = 0.3;
Zeta2 = 0.7;
t = 0:0.1:15;
s = tf('s');
P1 = K*w01^2/(s^2 +2*Zeta1*w01*s +w01^2);
P2 = K*w01^2/(s^2 +2*Zeta2*w01*s +w01^2);
P3 = K*w02^2/(s^2 +2*Zeta1*w02*s +w02^2);
P4 = K*w02^2/(s^2 +2*Zeta2*w02*s +w02^2);
f = figure;
f.Position = [100 100 700 375];
p = stepplot(P1,'k-',P2,'k--',P3,'k:',P4,'k-.',t)
grid
set(findall(gcf,'type','line'),'linewidth',1);
set(gcf, 'Renderer', 'painters');
p.AxesGrid.XUnits = '';
legend('$\zeta = 0,3$ ; $\omega_0 = 1$','$\zeta = 0,7$ ; $\omega_0 = 1$','$\zeta = 0,3$ ; $\omega_0 = 2$','$\zeta = 0,7$ ; $\omega_0 = 2$','interpreter','latex')
xlabel('Time (s)')
ylabel('Amplitude')
title('')
Guess what? I want it for a publication, i.e., it must be vectorial.
I tried all possible solutions inside Matlab figure (save as, export, changed parameters...). Also tried export_fig and matlabfrag -- which are very good tools, I admit. They just didn't solve the issue.
export_fig seems to have more options, so I'll detail a bit more what happens with it. Using the code above all the lines seems solid after some time. If I change from 'painters' to 'openGL', then the lines appear the way it should be. However, in this case there's no sense to use export_fig using 'painters' again (and obviously, it doesn't work either). Saving as .png with 'screen' resolution is not an option.
Tired of trying with vectorials, I gave up and decided to use .png with higher resolution (600 dpi) both inside the figure environment and also using export_fig. Yes, it seems to work, but the spaces and lengths of dashes changed too! And the lines in the legend look horrible, mainly the dotted.
I just wanted a vectorial figure in the way it pops up to me when it opens. Am I asking too much?
9 Comments
Adam Danz
on 27 Jan 2023
Could you share the line you used to export the image using exportgraphics?
For example,
exportgraphics(gcf,'test.eps','ContentType','vector')
Rodrigo
on 27 Jan 2023
See exportgraphics. To export in vector format, choose a filename with an extension that supports vectorized images (e.g. svg, pdf, etc) and specify ContentType as 'vector'.
Example
exportgraphics(gcf,'test.eps','ContentType','vector')
Adam Danz
on 30 Jan 2023
I ran the code in your question to produce the plot. This helps us see the problem you mentioned in the comment above regarding dashed lines. I was able to reproduce the bug in MATLAB Online and reported the issue. I'll add an answer shortly to share a workaround.
Accepted Answer
More Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
