How can I change the style of the axis lines? The a portion of the y-axis coincide with the curve being plotted. So I would like to have the y-axis in a different style (dashed maybe) so that the curve can be identified (plotting the curve in another color is not an option because ultimately the figure will be included in a black and white document). Any ideas?
clc;
close all;
clear all;
pa = 6;
upper_x = 40;
aut_x = 32;
upper_bnd_x=1.1*upper_x;
hours = 1200;
h_x = hours/upper_x;
upper_y =pa*upper_x;
upper_bnd_y=1.1*upper_y;
xlb=0;
xub=upper_bnd_x;
xubtick=upper_x;
n1 = 3;
x2 = linspace(0,upper_x,n1);
y2 = upper_y - pa*x2;
s21 = linspace(0,pa,n1);
s22 = pa.*ones(1,n1);
s23 = linspace(pa,2*pa,n1);
s2 = cat(2,s21,s22,s23);
xs21 = zeros(1,n1);
xs22 = linspace(0,upper_x,n1);
xs23 = upper_x.*ones(1,n1);
xs2 = cat(2,xs21,xs22,xs23);
dt2 = upper_x.*ones(1,n1);
dy2=linspace(pa,4,n1);
dx2=19+2.*dy2;
dt22 = aut_x.*ones(1,n1);
figure
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 2.8 2]);
plot(xs2,s2,'Color','black');
xlabel('measure x')
ylabel('measure y')
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XLim',[xlb xub],...
'XTick',[0,aut_x,xubtick],...
'YTick',[pa],...
'YLim',[0 2*pa],...
'Box','off')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
hold on;
plot(dt2,s21,':','color','black');
plot(dt22,s21,':','color','black');
hold off;

 Réponse acceptée

Star Strider
Star Strider le 26 Sep 2015
Modifié(e) : Star Strider le 26 Sep 2015

1 vote

You can change the grid lines by setting the GridLineStyle property. The box itself you can only turn on and off.

10 commentaires

John
John le 27 Sep 2015
Modifié(e) : John le 27 Sep 2015
I suppose it would change the grid but not the axis. I will edit the question to include a mwe for concreteness.
You can plot your own box lines as you would any other lines, then turn the box off:
x = linspace(-5, 5);
y = x.^2 - 3*x + 2 + 2*sin(2*pi*x);
figure(1)
plot(x, y)
xl = xlim;
yl = ylim;
hold on
plot([xl(1) xl(1)], yl, '--r', 'LineWidth',1)
plot(xl, [yl(2) yl(2)], '--r', 'LineWidth',1)
plot([xl(2) xl(2)], yl, '--r', 'LineWidth',1)
plot(xl, [yl(1) yl(1)], '--r', 'LineWidth',1)
hold off
set(gca, 'Box', 'off')
Experiment with the other options to get the appearance you want.
John
John le 27 Sep 2015
I really thought this would work out, so I accepted it as the answer. But as it turns out, I couldn't get it to work. The reason is that the lines being plotted in place of the axes would "overwrite" the original plot:
clc;
close all;
clear all;
pa = 6;
upper_x = 40;
aut_x = 32;
upper_bnd_x=1.1*upper_x;
hours = 1200;
h_x = hours/upper_x;
upper_y =pa*upper_x;
upper_bnd_y=1.1*upper_y;
xlb=0;
xub=upper_bnd_x;
xubtick=upper_x;
n1 = 3;
x2 = linspace(0,upper_x,n1);
y2 = upper_y - pa*x2;
s21 = linspace(0,pa,n1);
s22 = pa.*ones(1,n1);
s23 = linspace(pa,2*pa,n1);
s2 = cat(2,s21,s22,s23);
xs21 = zeros(1,n1);
xs22 = linspace(0,upper_x,n1);
xs23 = upper_x.*ones(1,n1);
xs2 = cat(2,xs21,xs22,xs23);
dt2 = upper_x.*ones(1,n1);
dy2=linspace(pa,4,n1);
dx2=19+2.*dy2;
dt22 = aut_x.*ones(1,n1);
figure
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 2.8 2]);
plot(xs2,s2,'Color','black');
xl = xlim;
yl = ylim;
hold on
plot([xl(1) xl(1)], yl, '--w', 'LineWidth',1)
xlabel('measure x')
ylabel('measure y')
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XLim',[xlb xub],...
'XTick',[0,aut_x,xubtick],...
'YTick',[pa],...
'YLim',[0 2*pa],...
'Box','off')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
plot(dt2,s21,':','color','black');
plot(dt22,s21,':','color','black');
hold off;
I also tried to solve the problem by getting the plot line thicker. But the part of the plot that is over the y-axis didn't get thicker:
clc;
close all;
clear all;
pa = 6;
upper_x = 40;
aut_x = 32;
upper_bnd_x=1.1*upper_x;
hours = 1200;
h_x = hours/upper_x;
upper_y =pa*upper_x;
upper_bnd_y=1.1*upper_y;
xlb=0;
xub=upper_bnd_x;
xubtick=upper_x;
n1 = 3;
x2 = linspace(0,upper_x,n1);
y2 = upper_y - pa*x2;
s21 = linspace(0,pa,n1);
s22 = pa.*ones(1,n1);
s23 = linspace(pa,2*pa,n1);
s2 = cat(2,s21,s22,s23);
xs21 = zeros(1,n1);
xs22 = linspace(0,upper_x,n1);
xs23 = upper_x.*ones(1,n1);
xs2 = cat(2,xs21,xs22,xs23);
dt2 = upper_x.*ones(1,n1);
dy2=linspace(pa,4,n1);
dx2=19+2.*dy2;
dt22 = aut_x.*ones(1,n1);
figure
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 2.8 2]);
plot(xs2,s2,'Color','black','LineWidth',2);
xlabel('measure x')
ylabel('measure y')
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XLim',[xlb xub],...
'XTick',[0,aut_x,xubtick],...
'YTick',[pa],...
'YLim',[0 2*pa],...
'Box','off')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
hold on;
plot(dt2,s21,':','color','black');
plot(dt22,s21,':','color','black');
hold off;
I didn’t see plotted data at x=0 in the plot from the upper code, so I can’t figure out what the problem is.
In the lower code you might be able to show the data plot more easily by ‘fudging’ 'XMin' a bit:
'XLim',[xlb-0.5 xub],...
Here, I subtracted 0.5 from it to nudge it a little to the left.
You will likely have to experiment with the axis limits and the positions of the plotted box as part of the solution.
There are probably more palatable approaches, though. Going back to my sample code, see if variations on these themes suggest a more general solution:
x = linspace(-5, 5);
y = x.^2 - 3*x + 2 + 2*sin(2*pi*x);
figure(1)
plot(x, y)
xl = xlim;
yl = ylim;
hold on
plot([xl(1) xl(1)], yl, '--r', 'LineWidth',1)
plot(xl, [yl(2) yl(2)], '--r', 'LineWidth',1)
plot([xl(2) xl(2)], yl, '--r', 'LineWidth',1)
plot(xl, [yl(1) yl(1)], '--r', 'LineWidth',1)
hold off
set(gca, 'Box', 'off', 'XColor','w', 'YColor','w')
set(gca, 'YTick', [0 22.5 45], 'XTick', 2)
text([1; 1; 1]*xl(1)-0.1, [0 22.5 45], {'0', '22.5', '45'}, 'Color','k', 'HorizontalAlignment','right')
text(2, yl(1)-0.2, {'2'}, 'Color','k', 'HorizontalAlignment','center', 'VerticalAlignment','top')
Notice that I set the colours of the X and Y axes, ticks, and tick labels white to turn them essentially invisible. I then used text objects to replace the axis tick labels but not the ticks themselves. (If you need the ticks, they’re pretty easy to add. They’re a variation on the code that plotted the dashed box borders.)
I didn’t comment this code, but you’re sophisticated enough to understand what I did from the code. I made it as clear as I could. I’ll be glad to follow up on this in the morning. For those who Accept my Answers, I keep working until we either get a suitable final result (optimal), we exhaust my expertise (occasionally), or we encounter a problem that completely resists our best efforts to solve it.
John
John le 30 Sep 2015
Thank you, sir. Your ideas helped me. I did get what I wanted "on the matlab screen". But when I save in .eps, the outcome is different. I'm posting here so you can see what may be the problem.
clc;
close all;
clear all;
pa = 6;
upper_x = 40;
aut_x = 32;
upper_bnd_x=1.1*upper_x;
hours = 1200;
h_x = hours/upper_x;
upper_y =pa*upper_x;
upper_bnd_y=1.1*upper_y;
xlb=0;
xub=upper_bnd_x;
xubtick=upper_x;
n1 = 3;
x2 = linspace(0,upper_x,n1);
y2 = upper_y - pa*x2;
s21 = linspace(0,pa,n1);
s22 = pa.*ones(1,n1);
s23 = linspace(pa,2*pa,n1);
s2 = cat(2,s21,s22,s23);
xs21 = zeros(1,n1);
xs22 = linspace(0,upper_x,n1);
xs23 = upper_x.*ones(1,n1);
xs2 = cat(2,xs21,xs22,xs23);
dt2 = upper_x.*ones(1,n1);
dy2=linspace(pa,4,n1);
dx2=19+2.*dy2;
dt22 = aut_x.*ones(1,n1);
figure
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 2.8 2]);
plot(xs2,s2,'Color','black');
hold on;
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XLim',[xlb xub],...
'XTick',[0,aut_x,xubtick],...
'YTick',[pa],...
'YLim',[0 2*pa],...
'Box','off');
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1],'Box','off');
plot(dt2,s21,':','color','black');
plot(dt22,s21,':','color','black');
xl = xlim;
yl = ylim;
plot([xl(1) xl(1)], [pa upper_bnd_y], '--w');
plot([xl(1) upper_bnd_x], [yl(1) yl(1)], '--w');
xlabel('measure x')
ylabel('measure y')
hold off;
Star Strider
Star Strider le 30 Sep 2015
I have no recent experience with saving to eps. I would see if the File Exchange contribution export_fig (very popular) can help.
John
John le 30 Sep 2015
The file extension is immaterial here. The output .fig does look fine when I see in matlab, that is, axis lines are dashed as intended. But any other format, .pdf, .eps, .jpg, .bmp, does not portray that.
This seems to work for me (with the plot in your latest code, and appended just after it):
saveas(gcf, 'John.png');
J = imread('John.png');
figure(2)
image(J)
I had to enlarge it manually to see everything, but it all seemed to be there (in R2015b).
If this doesn’t work as you want it to, I would contact MathWorks Tech Support for help.
John
John le 30 Sep 2015
I thank you very much, Star Strider. You've gone through more loops with me than I ever expected.
Here is the bottom line: I was using R2014a and it didn't work there at all! Since I could get R2015, I did and it did work there as you told me it would.
I don't know why this was the case, but I will keep both versions installed, even if it is a burden to my smallish SSD drive.
Incidentally, it seems that MathWorks finally ditched the ubiquitous "student version of matlab" that spoiled every single .pdf (thumbs up to them!)
Star Strider
Star Strider le 30 Sep 2015
My pleasure.
This was an interesting problem. I still have R2014a on this machine (the ‘old’ handle graphics are easier to use in some situations), but I used R2015b here.
I use the PDF suite produced by Tracker Software whenever I have to do a PDF of anything. I’ve used it for years (with updates), and like it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Measurements and Feature Extraction dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by