plotyy function showing two x-axes but only want one - matlab

I am using the plotyy function to produce the chart in the image below.
I have two issues with this chart. First issue being having two x-axes I want the zero on the left hand side of my chart to be level with the zero on the right hand side. Is there anyway I can make this happen?
Lastly I want to put some labels on the x-axis however you can that I labels have numbers on top of them. I only want the labels to be visible which I cannot seem to do?
Below is my code.
x_labels = data_cell(2:end, 1);
risk_tot = cell2mat(data_cell(2:end, 2));
risk_cont = cell2mat(data_cell(2:end, 3));
[pp,h1,h2]=plotyy((1:length(risk_tot)),risk_tot,(1:length(risk_tot)),risk_cont,'bar','stem');
set(gca,'XtickL',x_labels);
set(h1,'FaceColor',my_Blue2(40,:),'EdgeColor',my_Blue2(40,:))
set(h2,'Color',my_Orange(1,:),'LineWidth',0.5,'MarkerEdgeColor',my_Orange(1,:))
set(pp(1),'Box','off')
set(pp(2),'Box','off')
Update
I have managed to resolve the second issue with the x-axis labels. I simply added the line below. This sets the second x-axis labels to empty.
Still can't fix the first issue.
set(pp(2),'XTickLabel',[]);

Try linkaxes:
linkaxes(pp,'y')
You may need to adjust the axes limits with pp(1).Ylim = ... to get the desired result.

Related

How to force a line break in a legend of a boxplot?

I need help to skip lines in the legend of boxplots : I have a plot with two boxplots, and each of them has a legend to long to fit on a single line.
I tried this, X being my matrix of data (two colums) :
boxplot(X,'Labels',{'1stpartofthelegend\newline2ndpartofthelegend','1stpartofthelegend\newline2ndpartofthelegend})
This solution works in other types of plots, like a plotspread plot so I don't understand why it is not working here ?
I found this question, but the solutions are not working for me, maybe because I have a more recent version (R2018b).
I actually want to add my boxplots over a plotpsread plot, but when I do that the legends of the boxplots win over the legends of the plotspread (even if I add boxplots without legends, the previous legends of the plotspread are replaced by "1" and "2"). So preventing the boxplot to erase the previous legend would be a nice solution but as I failed to do that I tried to find another way.
Thank you for your help
You want to set the 'Labels' of the boxplot for multiple lines. So use a cell of cells:
Lbl = { {'1st entry (1st line)','2nd entry (1st line)'},...
{'1st entry (2nd line)','2nd entry (2nd line)'} };
boxplot(rand(100,2),'Labels',Lbl)
EDIT
LaTeX commands work in the title and labels but apparently there is no interpreter or TickLabelInterpreter property for boxplot-Labels:
Error using internal.stats.parseArgs (line 42)
Invalid parameter name: TickLabelInterpreter.

Custom number format for y-axis on Chart

I have created a chart with 2 axes that acts as a panel chart (see image)
As a panel chart I only want to show the portions of the relevant y-axes to the chart next to them. For example, for the right-most y-axis I used a custom number format to exclude anything less than 0:
_(* #,##0_);_("";_(* 0??_);_(#_)
But for the left most y-axis, I'm stuck. I want to show -400 to positive 400. I've tried 2 different options, but neither is producing the desired effect.
[<0](#,##0);[>500000000]"";#,##0_)
[<0](#,##0);[<500000000]#,##0_);""
Here is the result I'm looking for:
I learned something new today (and a bit weird) regarding formats and chart axes
After some experimenting, this is what I ended up using:
[White][>500]_(#,##0_);(#,##0);0;
The odd part: When you change the Display Units of the axis (for me, millions), then the formatting no longer recognizes the original amount (500,000,000).
Once I figured that out, I was able to work out the solution.

Stair plot to vertical bar plot in Matlab

I want to create a vertical bar plot. This is my code:
bar (x, sensiv);
title ('Promedio X')
xlabel('Nm')
ylabel('Refl.')
The problem is it looks like a stair plot. I've tried to add (x,sensiv, 'stacked') but it doesn't work. It looks grouped, as you can see in the next image:
graph http://imageshack.us/a/img689/9449/capturawv.jpg
I think it's because of x-axis but I couldn't change it. How can I do it? Does somebody knows how can I do it?
EDIT
Thanks Colin! I've tried to zoom and I understand what you mean and I've tried with different values, as slayton and you said.
I think that maybe it's the way I've code the plot, it is possible?
abc=0;
for p=(61:201)
abc(p)=out1_c;
end
for p=(151:301)
abc(p)=out2_c;
end
for p=(231:380)
abc(p)=out3_c;
end
for p=(381:596)
abc(p)=out4_c;
end
for p=(1152:1531)
abc(p)=out5_c;
end
for p=(1651:2051)
abc(p)=out7_c;
end
for p=(2052:2151)
abc(p)= 0;
end
The default value for the width of the bars in a bar plot is 0.8, so given that you're not currently specifying the width, you should have gaps in between each bar. This is going to sound really obvious, but have you tried zooming in on the bar plot that is created? For some datasets, the bar function will return a plot that looks like a stair plot, but in fact has gaps if you zoom in far enough. If this is the case, then you should be able to get the gaps you want by tinkering with the width parameter as suggested by slayton.
EDIT
Okay. First things first. If you want to post additional information, you should add it to your question, NOT post it as a new answer! You can do this by clicking the edit button just below where your question is on the page. To make things more readable, you might preface your edit with a capitalized bold-face heading "EDIT" as I have done here. If you are able, try now to move the additional information you've given back into your question, and then delete the answer.
Second, I have to be honest, the additional information you posted was somewhat confusing. However, I think I understand what you want now. You want 7 bars coming up to the heights out1_c, out2_c, ..., out7_c (variable names taken from your additional information) with a small gap between each bar, and the x-axis to reflect (approximately) the intervals 450-550, 550-650, etc.
Well, if you want 7 bars, then you want your input to only have seven elements. Set:
y = [out1_c; out2_c; out3_c; out4_c; out5_c; out6_c; out7_c];
y now gives you the heights your bars will come up to on the y-axis. To locate the bars on the x-axis, define a vector x that also has seven elements, where each element gives the midpoint of where you want the bar to be on the x-axis. For example:
x = [100; 200; 300; 400; 500; 600; 700];
Then just use bar(x, y). This should get you started.
A final point on the code you posted, you can actually completely avoid the loops: read up on vectorization. But if you are going to insist on loops, the first and most important rule is to preallocate your vectors/matrices. In your code abc starts out as a scalar (a 1 by 1 matrix), but then for every p, you are adding an element at index p. What is actually happening in the background is for every p, matlab is scrapping the current abc you have in memory, and building it again from scratch with the additional element. As you might expect, this will slow down your code by many orders of magnitude.
You can set the width of the individual bars by passing a value between 0 and 1 to bar. Passing 1 indicates that there should be no space between the bars
bar(x,y,1)
Passing anything less than 1 will reduce the bar sizes and introduce spacing between the individual bars
bar(x,y,.5)

Sigmaplot: How to scale x-axis for correctly displaying boxplots

I want to display overlapping boxplots using Sigmaplot 12. When I choose the scale for the x-axis as linear then the boxes do indeed overlap but are much too thin. See figure below. Of course they should be much wider.
When I choose the scale of the x-axis to be "category", then the boxes have the right width, but are arranged along each single x-value.
I want the position as in figure 1 and the width as in figure 2. I tried to resize the box in figure 1 but when I choose 100% in "bar width" than it still looks like Figure 1.
many thanks!
okay, I found the answer myself. In Sigmaplot, there is often the need to prepare "style"-columns, for example if you want to color your barcharts, you need a column that holds the specific color names.
For my boxplot example I needed a column that has the values for "width". These had to be quite large (2000) in order to have an effect. Why ? I have no idea. First I thought it would be because of the latitude values and that the program interprets the point as "1.000"s, but when I changed to values without decimals, it didnĀ“t get better.
Well, here is the result in color.
Have fun !

asp.net charts: Legend overlapping with X-axis

I am creating a Chart (DataVisualization.Charting.Chart) programmatically, which is a Stacked Bar chart.
I am also adding Legend entries programmatically to it. I want to show the Legend at the bottom of the chart.
But, while doing so, the Legend overlapps with the X-axis of the chart.
Here is the code I am using:
Private Function GetLegend(ByVal legendName As String, ByVal s As Single) As System.Windows.Forms.DataVisualization.Charting.Legend
Dim objLegend As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()
objLegend.Name = legendName
objLegend.Font = New System.Drawing.Font("Verdana", s)
objLegend.IsDockedInsideChartArea = False
objLegend.Docking = Docking.Bottom
Return objLegend
End Function
Below statement adds that Legend to the chart
_msChart.Legends.Add(GetLegend("SomeValue1", 10.0F))
Any idea, what is missing? I want to show the legend at the bottom only, but it should not overlapp with the X-axis.
I had the same problem today. Try adding:
objLegend.Position.Auto = true
objLegend.DockedToChartArea = "yourChartAreaName"
That did not help me but I found on the net that this might be helpful (and clean solution).
What actually worked for me was moving chart area to make space for legend so it no longer overlaps. My legend was on top so this code worked for me:
chart.ChartAreas[0].Position.Y = 15
You can try resizing it instead, forcing it to be for example 20 pixels shorter than chart.Size.
Hope this helps.
I had an overlapping legend/chart area problem as well but none of the other suggestions here seemed to make any difference. I think the problem stems from legend text wrapping to two lines and the sizing algorithms not taking account of this.
The ideas here got me thinking more clearly about the problem though, and I was able control the size and position of the chart area using the following.
Chart1.ChartAreas[0].InnerPlotPosition = new ElementPosition(15, 5, 90, 75);
There's not much intellisense on those parameters, but as well as I could deduce, the parameters are all percentages of the total chart area (I initially thought they might be pixel values and got some very odd results). So what I've written above would set the plot area to start at 15% in from the left edge of the chart image and 5% down from the top, and have a width of 90% and a height of 75%.