Large matlab plot doesnt display properly when using colours - matlab

I have a large amount of data which I have plotted on a bar chart. The default is that the bars are all blue with no spaces. However when I try to add colours white lines appear on the solid blocks of colour. Why is this and is there a way to get rid of them?
n=3000
mydata=2-abs(randn(n,1));
x=zeros(n,1);
x(1:2*n/10)=1;
x(2*n/10+1:n/2)=2;
x(n/2 +1:6*n/10)=3;
x(6*n/10 +1:8*n /10)=4;
x(8*n/10 :n)=5;
data=[x,mydata];
data=sortrows(data,[1,2])
bar=bar(data(:,2));
bar_child=get(bar,'Children');
set(bar_child,'CData',data(:,1));
set(gca,'ytick',[])
set(bar_child, 'EdgeColor', 'none')
You can see the difference clearly if you try running it without the last 4 lines
bar_child=get(bar,'Children');
set(bar_child,'CData',data(:,1));
set(gca,'ytick',[])
set(bar_child, 'EdgeColor', 'none')

You're right - when you run the above without the last four lines, all bars are blue and you can't see any vertical white lines separating in the plot. However, if you zoom in, you can start to see the white lines. So they are there in other plot, just not as noticeable.
I'm not sure why this is happening. More vertical white lines appear as I resize the multi-colour plot.
A workaround is as follows. Since the default bar width is 0.8, change this to 1.0 so that there is no separation between bars
bar=bar(data(:,2),'BarWidth',1);
and re-run all lines of code. When I did this, the results looked fine (no vertical white lines).
Try this and see what happens!

Related

How to draw rectangle around tiled object in a MATLAB plot?

I created a tiledlayout(4,3)-object and I want to emphasize the 8th tile by a surrounding line or changing the backgroundcolor (not of the graph itself, but just the greyspace behind the graph, labels, axes etc.).
I could not find anything that changes the backgroundcolor of just one tile, so I tried to draw a rectangle around the tile and color it.
I executed the code below and expected to get a red line around the OuterPosition- or the TightInset-position, but nothing happened. The code created the tiles without any error-codes and there was no red rectangle around the 8th tile.
Can anyone tell me how to color the background of a tile or get a rectangle around it?
Thank you very much!
ax = nexttile(8)
lines_around_tile = get(ax, 'OuterPosition'); %'TightInset')
rectangle('Position', lines_around_tile, 'EdgeColor', 'r', 'LineWidth',10)
I got this result.
Result
To draw a rectangle, run the code and in matlab figure (with all the tiles) go to view -> Plot Edit Toolbar. It generates a toolbar where you can find lines, rectangles, circles...
Then insert your rectangle, right click on it, Property Inspector. In this window you can change the facecolor, linecolor, linewidth...

Is there a way to make matlab gridlines bold?

I know that I can set the color of gridlines using:
pax = gca;
pax.GridColor = 'black';
But the gridlines are still a faint grey. I would like to make them darker. Is there a way to make the gridlines bold?
Note I do not want to make the gridlines thicker, I just want to make them darker that the faint grey that seems to be the automatic when using black
I am using polar plots and when I print them, the grids are almost too faint to see.
You need to modify the GridAlpha property as well so that the grid lines are actually opaque. The default value is 0.15 so even though they are black they appear gray since they are transparent. You'll want to use a GridAlpha value of 1 .
pax.GridAlpha = 1

Matrix Semilogx linestyle and linewidth change

I need some help making some changes in the way my plot is plotted with this code.
First of all, I need dark colours. I want my the lines to start from black and slowly go to dark red/purple. Scaling from black to dark red/purple smoothly.
The other thing I need is on how to get other variations in my linestyle. I stole the linestyle code from somewhere and it is kinda useful, but I need more than the 4 variations given.
I also wanna add another line, with semilogx again and add it on the legend. How can I achieve that without messing with the previous legend? If I just add the line and put an extra line on the legend, it gives me the wrong color. (if I just use hold on and add an ,'', in the legend it gets a parula color and not the actual color)
fig = semilogx(SMPSFIN(1,5:75),z);
numLines = length(fig);
cmap = colormap(parula(numLines));
style = {'-','--',':','-.'};
for i = 1:numLines
fig(i).Color = cmap(i,:);
set(fig(i),'Linewidth',i*sqrt(1.5*i)*0.1);
set(fig(i),'linestyle',style{1+int8(mod(i,4))});
end
Thanks a lot for your time :)

Plot lines get under the X-axis in Core Plot

I've changed the look of my plots, and now the line of the plots accidentally goes under the X-axis. The problem looks like this (notice the black lines under the axis):
How can I get rid of it? I've change the plot line like this:
aaplPlot.interpolation = CPTScatterPlotInterpolationCurved;
Plots do not draw outside the plot area. You can control its size by setting padding on the plot area frame to push the edges of the plot area inwards. You will also need to adjust the plot ranges of the plot space to keep the same appearance.

double axis used in matlab

I am using a plotyy in matlab to plot two data set in a same figure. The left and right axis are of the different range. But I just find that on the right axis, there seems to be two different set of scale shown. I think one of them is really from the left axis.
t=-1:0.02:1;
y=sin(t);
y1=2*sech(t);
[AX, H] =plotyy(t, y, t, y1);
ylim(AX(2), [0 3.25]);
set(AX(2), 'YTickMode', 'auto')
After searching that online, I found that to turn off the box will solve the problem too. But the problem is to turn the box off will case the top horizontal line gone also. Is that anyway to remove the extra scale and keep the frame? Thanks.
It is possible and not terribly difficult, here's an illustrative example figure based on your test code
What I've done is to add a third axis (in this case I accomplished this by taking a shortcut - I called plotyy twice resulting in an extra blue line in the first axis and an extra second axis with a green line).
The idea is to turn the bounding box off for both the first and second axes, and then to turn it on for the third. That results in the top axis giving you the left vertical axis, the second the right vertical axis, and the third the top horizontal axis.
I don't think there's simple a way to do what you want. If you turn off the box (to get rid of the blue ticks on the right hand side) then the top horizontal line will disappear:
set(AX(1), 'Box','off')
If you want you could re-draw the line back in with:
line([-1, 1], [1, 1])
Or more generally:
lims = get(AX(1),{'XLim','YLim'});
line(lims{1}, lims{2}([2 2]))