No way to plot correctly dashed and dotted lines using painters

125 次查看(过去 30 天)
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 个评论
Rodrigo
Rodrigo 2023-1-27
移动:Adam Danz 2023-1-29
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 2023-1-30
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.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2023-1-30
编辑:Adam Danz 2023-2-23
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 个评论
Rodrigo
Rodrigo 2023-1-30
编辑:Rodrigo 2023-2-23
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by