No way to plot correctly dashed and dotted lines using painters

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

Could you share the line you used to export the image using exportgraphics?
For example,
exportgraphics(gcf,'test.eps','ContentType','vector')
Sorry, I used directly the Export tool in Matlab figure...
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')
Rodrigo
Rodrigo 2023년 1월 27일
이동: Adam Danz 2023년 1월 29일
There's an error message: Undefined function 'exportgraphics' for input arguments of type 'matlab.ui.Figure'.
I tried with gcf, gca, f and p... What am I missing?
Adam Danz
Adam Danz 2023년 1월 27일
이동: Adam Danz 2023년 1월 29일
I just saw that you are using MATLAB R2019a. exportgraphics was released in R2020a. What file format are you trying to export to?
Adam Danz
Adam Danz 2023년 1월 27일
이동: Adam Danz 2023년 1월 29일
If you have access to MATLAB Online you could try using exportgraphics there.
Rodrigo
Rodrigo 2023년 1월 27일
이동: Adam Danz 2023년 1월 29일
I'm trying either .pdf or .eps. Using MATLAB Online for all figures (I have dozens) would be complicated. But I'll try it anyway since I've seen no satisfactory answer for this issue yet.
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.
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개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2023년 1월 27일

편집:

2023년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by