How do I make the second graph look like the first?

1 visualización (últimos 30 días)
PromoCodeBSJ
PromoCodeBSJ el 1 de Oct. de 2018
Comentada: PromoCodeBSJ el 2 de Oct. de 2018

Hi I am writing code to automatically generate graphs from an excel file and am wondering how I miraculously made the first graph look how it is and not the second one.

<<

>>

I would like the second graph to be all the same colour with different line styles. How do I do this? Here is an excerpt of my code

freqCounter=1;
tempCounter=1;
legendCounter=1;
firstDataSheet=2;
sheet=firstDataSheet;
PvsA=figure('Name','Power versus Current','NumberTitle','off');
SlopevsA=figure('Name','Slope (W/A) versus Current','NumberTitle','off');
cmap=colormap(lines(combinations));
for k=1:combinations
      %need to modify the right hand side of the colon to find end of the
      %data 
      Current= xlsread(path,sheet,['B2:B' num2str(unique+1)]);
      PeakPower= xlsread(path,sheet,['E2:E' num2str(unique+1)]);
      beepboop=xlsread(path,sheet,['H5:H' num2str(unique+1)]);
      offsetLambda=xlsread(path,sheet,'H2');
      deltaT=(beepboop-offsetLambda)/lambdaK; %pre offset
      offset=deltaT(1);
      deltaT=deltaT+offset*-1;
      Slope=PeakPower/Current;
      figure(PvsA)
      yyaxis left
      plot(Current,PeakPower)
      hold on
      yyaxis right
      plot(Current(4:end),deltaT)
      hold on
      figure(SlopevsA)
      plot(Current,Slope)
      hold on
      sheet=sheet+sheetsToSkip;
end
%adding labels to the graph
figure(PvsA)
title('Peak Power versus Current and delta T vs Current')
yyaxis left
xlabel('Current (A)')
ylabel('Peak Power (W)')
yyaxis right
ylabel('delta T')
  6 comentarios
PromoCodeBSJ
PromoCodeBSJ el 1 de Oct. de 2018
I want to make it so it automatically changes linestyle. I didn't add any code into the first graph that did that, but it came out nicely. I am wondering how I can replicate it. I do not know how many line I have to plot so a hardcoded solution will not work.
jonas
jonas el 1 de Oct. de 2018
Interesting... it seems to be a feature of the yyaxis... I'll formulate an actual answer instead.

Iniciar sesión para comentar.

Respuesta aceptada

jonas
jonas el 1 de Oct. de 2018
Editada: jonas el 1 de Oct. de 2018
yyaxis automatically changes the linestyle and even inserts markers when plotting multiple lines. If you want to make the second plot appear as the first one, then I suggest you use yyaxis but hide one of the axes.
yyaxis left
plot(rand(10,10))
yyaxis right
set(gca,'ycolor','none')
You could also grab the default settings of the yyaxis in your first graph
cs=get(gca,'colororder');
ls=get(gca,'linestyleorder')
ans =
7×2 char array
'- '
'--'
': '
'-.'
'o-'
'^-'
'*-'
and then simply apply them after toggling to your second graph
set(gca,'linestyleorder',ls)
set(gca,'colororder',cs)
Note that the yyaxis by default only has one colororder and multiple linestyleorders, whereas the normal plot has one linestyleorder and multiple colororders. By default, the first linestyle is plotted with each color, then the second linestyle is plotted with each colors and so on... This behaviour is controlled by the colororderindex and linestyleindex.
  5 comentarios
jonas
jonas el 1 de Oct. de 2018
Editada: jonas el 1 de Oct. de 2018
If you want the plot to work exactly like the yyaxis does, then you have to change the colororder as well. The default yyaxis only has one color and multiple linestyles so as to match the yaxis color. The normal plot has mutliple colors and only one linestyle
axes;
get(gca,'colororder')
ans =
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.3010 0.7450 0.9330
0.6350 0.0780 0.1840
It seems the colororder is, by default, prioritized over the linestyleorder. Basically, the first linestyle is plotted with each color, then the second linestyle is plotted with each colors and so on... You can change this behaviour by playing around with the properties LineStyleOrderIndex and ColorOrderIndex.
PromoCodeBSJ
PromoCodeBSJ el 2 de Oct. de 2018
I seem to get some strange lines now. There are some weird lines at the y=0 horizontal. Would you know where this is from?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by