This code
1 function makegraph(A,B)
2 results=load(A);
3 time = results(:,3) - 1238370000;
4 firstTimeIndex = find(time >= (max(time) - 86400*7), 1);
5 results = results(max(1,firstTimeIndex-1):end, :);%Results now only containe s data from the last week
6 temp = results(:,3)-1238370000;
7 h=plot(temp,smooth(results(:,1)),':b','linewidth',2)
8 ylim([0 80])
9 xlim([max(temp)-(86400*7),max(temp)-1])
10 set(gca,'color','black')
11 set(gcf,'color','black') %get's rid of he axis alltogether
12 hold on
13 plot(temp, smooth(results(:,4)), 'r', 'linewidth', 2);
14 plot(temp, smooth(results(:,5)), 'g', 'linewidth', 2);
15 plot(temp, smooth(results(:,6)), 'm', 'linewidth', 2);
16 xlim([max(temp)-(86400*7),max(temp)-1])
17 set(gca,'XTick',[1:86400:(max(temp))+1])
18 set(gca,'XTickLabel',['Mon';'Tue';'Wed';'Thu';'Fri';'Sat';'Sun'])
19 print('-djpeg',B)
20 hold off
Saves this graph in the filename 'B'...
It works fine, but I'd like to put it in a different context, for which I need it to have a black background...
I have tried setting
set(gca,'color',[1 1 0])
set(gcf,'color',[1 1 0])
as described by Setting the background color of a plot in MATLAB using the command line?
and I've tried
whitebg(1,'k')
...and I'm getting nowhere -particualarly because sometimes when I try and play around with some of
set(gca,'color','black')
set(gcf,'color','black') settings, some of my plots disappear.
I'm deeply confused.. could someone tell me why the accepted answer at Setting the background color of a plot in MATLAB using the command line? isn't working here...?
Turns out that the commands were working within MATLAB, they just weren't working on the printed file because, according to http://www.mathworks.co.uk/help/matlab/ref/print.html....
By default, MATLAB changes the figure background color of printed
output to white, but does not change the color of uicontrols. If you
have set the background color, for example, to match the gray of the
GUI devices, you must set InvertHardcopy to off to preserve the color
scheme. To set InvertHardcopy on the current figure, use the command:
set(gcf,'InvertHardcopy','off')
So once I set set(gcf,'InvertHardcopy','off'), everything was peachy... thanks in particular to Molly, who put me on the right track...
Related
I'm currently trying to create a frequency histogram, and to do this, I had to create a bar graph that has no whitespace between the bars. However, this centers the XTickLabels in the middle of the bars. Since it's a histogram, I would like the numerical values to be at the line between each bars so that it can visually indicate intervals. Essentially, I need to shift all the tick labels to the left.
I'm using R2016a.
Looking through Mathworks only turned up answers for how to do this using line graphs, scatter plots, etc.
Use the Style argument of bar to set it to histc.
v = [6 12 17 21 28 25 19 15];
bar(v,'histc');
which gives:
Furthermore, if you're interested you can change the direction of tick marks to outwards with:
set(gca,'TickDir', 'out')
Have you tried to use the set command. For example:
%create some data
x=1:10;
y=rand(1,10);
%bar plot
bar(x,y)
%shift the x to the left 0.4
set(gca, 'XTick', x +.4)
I tried to google your problem and found this answer directly here:
XTick labels and Stacking in bar plot
Is there a reason why you aren't just using hist or the newer histogram? Investigating the source code for hist using edit hist I find that they are simply using bar with the 'hist' option. Something like this:
edges = 1:10
binwidth = 1
x = edges + binwidth/2
nn = 21:30
bar(x, nn, [min(edges), max(edges)], 'hist')
Gives
You can check the code for histogram as well to see how they are doing it there, but it's a little move involved.
Problem
I am plotting two different data sets in two different plots, and I want the graph in the same colour (blue). Obviously, I thought this is easily done by setting the colour of the markers and the lines, but although I use the same RGB code the plots appear with a different blue.
The illustrations below show the different appearance and this problem is the same if looked at in Matlab or exported as png, pdf, or jpeg. A colour print version also shows a different color. I know the colors are similar but if looked at properly they are not the same. And just to make sure, I do export them in the same size, same properties, same everything.
Question
Do I maybe have a simply plotting mistake ? Or is there a way to fix this ? I cant find a way to solve the problem.
Help is very appreciated!
Plot1
Plot2
Plot 3
Code 1
y=[0.3700 0.3600 0.350 0.3398 0.325 0.30]
h=plot(1:6,y,'Marker','o','Color',[ 0 0.4470 0.7410], 'MarkerFaceColor', [ 0 0.4470 0.7410], 'LineWidth', 1.5)
hold on
ax=gca;
ax.YLabel=ylabel('Test');
ax.XLabel = xlabel('Year');
l=legend('Whatever the legend says')
set([gca,ax.XLabel,ax.YLabel], 'FontName', 'Helvetica','FontSize', 8)
set(l,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'LineWidth',1.0)
hold off**
Code 2
h=figure
hold on
yyaxis left
l1=plot(1:6,1:6);
l2=plot(1:6,2:7);
hYLabel=ylabel('Test');
yyaxis right
r1=plot(1:6,3:8);
hYLabel=ylabel('Test');
hXLabel = xlabel('Test');
a=sprintf('TestTest\n Test');
b=sprintf('Test Test\n Test');
c=sprintf('Test & Test\nTest Test');
hLegend=legend([l1,l2,r1,], a, b, c);
set([gca,hXLabel,hYLabel,hLegend] , 'FontName' , 'Helvetica','FontSize', 8)
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'LineWidth',1.0)
ax=gca;
set(l1, 'LineWidth',1.5,'LineStyle','-','Color',[0 0.447 0.7410])
set(l2, 'LineWidth',1.5,'LineStyle','-.','Color',[0 0.447 0.7410])
set(r1, 'LineWidth',1.5,'LineStyle','-')
hold off
Recent versions of MATLAB use anti-aliasing when displaying and printing graphics (unless explicitly disabled), so it is possible that a thinner line of the same color may appear lighter than a thicker line of that same color.
You could optionally disable the anti-aliasing of a figure
set(gcf, 'GraphicsSmoothing', 'off')
Or globallly for all figures
set(0, 'DefaultFigureGraphicsSmoothing', 'off')
All of the images you have posted above are the same shade of blue when you use a pixel color inspection tool rather than relying on your eye.
This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I have a contour plot with data that goes from -90 to 90 degrees. for now i am using jet so I have a map that looks like this
I have been asked to change the colormap so that instead of having a gradient, I have a fixed color for each 5 degress (so I believe 36 colors). Also i was thinking of maybe having same colors for the interval [5 10] and [-10 -5], and so on if that makes sense.
My code is quite long because i have a lot of data to process, but that's part of it just so you can see what function i am using to plot this
%%
x1=data(:,5); %x location
y1=data(:,16); %y location
z1=phi*90; %angle phi
z2=gamma*90; %angle gamma
n=300; precision of grid
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
figure(3);
contourf(X,Y,griddata(x1,y1,z1,X,Y),100,'EdgeColor', 'None')
%title('Variation of In-plane angle \phi')
axis equal
axis ([0 8000 0 12000])
axis off
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:15:90])
Does anyone know how to create this colorbar?
Cheers
Every colormap-generating function in Matlab, including jet, takes an argument that specifies how many colormap entries there should be. In your case, you want 180 / 5 = 36 discrete colors:
colormap(jet(36))
To make sure the 36 colors cover exactly the 5 degree steps, set the color axis explicitly:
caxis([-90 90])
The result looks e.g. like this:
I have the following MATLAB code and I'm trying to make all of the plot traces black:
x = 20:0.01:30;
m1 = 25;
s1 = 2.5;
pdfNormal_1 = normpdf(x, m1, s1);
m2 = 25.478;
s2 = 0.1637;
pdfNormal_2 = normpdf(x, m2, s2);
m3 = 25.478;
s3 = 0.189;
pdfNormal_3 = normpdf(x, m3, s3);
set(gcf,'color','w');
g=findobj(gca,'Type','patch');
%set(g(1),'FaceColor',[0 .5 .5],'EdgeColor','w')
%set(g(2),'FaceColor',[0 1 1],'EdgeColor','w')
%set(g(3),'FaceColor',[0 1 1],'EdgeColor','w')
set(gca,'Fontsize',12,'Fontname','euclid')
xlabel(' ') %tÃtulo eixo xx
hold all;
%plot(x, pdfNormal_1, x, pdfNormal_2, x, pdfNormal_3);
%set(gcf,'Color',[0 0 0])
plot(x,pdfNormal_1,'-.')
plot(x,pdfNormal_2,':')
plot(x,pdfNormal_3,'-','LineWidth',2)
Can someone help me? I removed the % from set(...);, but it plots nothing.
I don't quite know what exactly you want. You can either interpret this as:
You wanting all of your lines to be black
You wanting the background pane to be black.
Let's answer each question anyway so we have our bases covered.
Wanting all lines black
For the third parameter in plot, you can use a single letter that specifies what colour you want for your lines within your plot. Therefore, if you want all three of your plots black, use k after each invocation to plot. b is actually reserved for blue. Also, because you are calling plot more than once, each call to plot by default will overwrite the contents of the current figure with the latest invocation to plot, and so if you want all three plots to appear at the same time, you need to use hold on. Therefore, place this at the end of your code:
hold on;
plot(x,pdfNormal_1,'k-.')
plot(x,pdfNormal_2,'k:')
plot(x,pdfNormal_3,'k-','LineWidth',2);
You can also get rid of any set commands as these aren't useful. What you're actually doing is setting the background of the figure to white, which is probably not what you want to do. By background, I mean the area where the axes appear, not the drawing area of the plot itself.
Wanting the background pane black
If you want the background of where the plot appears to be black, it's a very simple one line statement. You need to set the current axes colour, not the current figure. Therefore, replace your set(gcf...) statement with set(gca...). Therefore:
set(gca,'Color',[0 0 0])
Now, if this is what you want, it'll be up to you to figure out what colours will appear on this plot nicely. Red certainly appears nice here!
For more information about how plot works, check out the documentation page on MathWorks: http://www.mathworks.com/help/matlab/ref/plot.html. It's actually very well explained!
I have a file with two columns
9 5
10 3
11 0
12 25
13 50
14 80
etc
What is the best way to plot bar char using gnuplot? Is using subprocess with gnuplot the best way? Ideally the graph should be in .pdf and in png as I want to put these on a website later on.
I would really appreciate your input/advise.
Here is an example, how to plot a bar chart with gnuplot using the plotting style with boxes
set terminal pngcairo size 1000,800 font ',12'
set output 'output.png'
set xlabel 'xlabel' font ',18'
set ylabel 'ylabel' font ',18'
set boxwidth 0.9
set style fill solid 0.3
set offset 0.5, 0.5, 10, 0
plot 'data.txt' with boxes linewidth 3 title ''
With the data you showed, this gives the following output image: