No way to plot correctly dashed and dotted lines using painters

132 views (last 30 days)
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)
p = resppack.timeplot
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
Rodrigo
Rodrigo on 27 Jan 2023
Moved: Adam Danz on 29 Jan 2023
Just tried in MATLAB Online. Same problem as previous. If I use painters, the figure is already with wrong dashes, and the exportgraphics doesn't change a thing. If I use openGL, the figure opens ok in MATLAB. But when using exportgraphics the issue happens again in the .pdf file.
Adam Danz
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.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 Jan 2023
Edited: Adam Danz on 23 Feb 2023
Thanks for reporting this issue. Here's a workaround I came up with after some trial and error.
After running the code from your question, move the curves to a new figure, set the decorations and the renderer, then export. I tested this in R2019a and MATLAB Online. The resultant pdf is attached and a screenshot of the vectorized pdf is below (the screenshot, of course, is not vectorized). Check that your legend strings are in the correct order after copying!
h = findall(f,'type','line');
iscurve = strcmpi({h.Tag},'Curves');
newfig = figure;
newax = axes(newfig);
hold on
newh = copyobj(h(iscurve),newax);
box on
grid on
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')
set(newfig, 'Renderer', 'painters') % not needed in recent releases; after legend
% Export
print('test.pdf','-dpdf')
Update: easier workaround
Alternatively, you can merely remove the z-data from all lines to solve this problem. Thanks again for reporting it.
% f is the original figure handle to the stepplot
h = findall(f,'type','line');
set(h, 'ZData', [])
  1 Comment
Rodrigo
Rodrigo on 30 Jan 2023
Edited: Rodrigo on 23 Feb 2023
Incredible, worked like a charm! I can't understand why you need another figure for that, but in the end it doesn't matter. I must congratulate you, Adam! You really solved an old issue that very few people did.
Update:
And it's now even better with Adam's update. Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!