I am plotting a histogram using matplotlib, my code is bellow. In the second plot if i set the histtype='step' I loose three bins of the data and cannot work out why. Has anyone had this problem before? If I change the histtype='bar' the plot looks fine.See image attached.
Code 1:
plt.hist(DelB[Sp],bins=20,histtype='step',normed=True,label='Sp')
dd=np.concatenate((DelB[S0],DelB[E]))
plt.hist(dd,bins=15,color='g',histtype='step',normed=True,label='E+S0')
plt.xlim(0.4,2.3)
ylabel('normalized fraction',size=15)
plt.legend()
Code2:
plt.hist(DelB[Sp],bins=20,alpha=0.5,facecolor='blue',normed=True,label='Sp')
dd=np.concatenate((DelB[S0],DelB[E]))
plt.hist(dd,bins=15,alpha=0.5,facecolor='green',normed=True,label='E+S0')
plt.xlim(0.4,2.3)
plt.legend()
ylabel('normalized fraction',size=15)
Your axis limits in the second plot are different. You can't see bars that are below the image boundary. Adding plt.ylim(0,2) will solve the issue
Related
I am making a program, which gives me a graph of users.
But it gives me something like this after the amount of dates has grown too high.
https://i.stack.imgur.com/iwSdc.png
I want to save them stretched, make them longer so you will be able to see dates properly.
Is there any parameter for this in plot.savefig('name')?
For example this data
Option 1
You could rotate tick labels using:
plt.xticks(rotation=90)
Option 2
or do it automaticly if you have date format:
fig.autofmt_xdate()
You can try following code:
plt.xticks(rotation=90)
It will print your x-axis values vertically.
Hope this helps!
I am trying to view the std error bar for each dataset but they are overlapping each other. Is there a way to stagger the error bar for each dataset?
Here is the code I am using:
group=hms.groupby([ hms.index.month]).mean()
std=hms.groupby([ hms.index.month]).std()
group.plot( linewidth=2,yerr=std)
[enter image description here][1]
Line Graph with Error bars
Seaborn's pointplot has that option baked in. Just set the parameter dodge to True.
You'll probably have to reformat your data into a "long" format though. Then create a new column with just the months to use as your x axis. I can't tell you exactly how without sample data.
sns.pointplot(x='month', y='value', hue='group', data=hms, ci='std', dodge=True)
Otherwise, you can just add shift your x values by some small amount for each group and use the standard matplotlib library.
How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).
I'm looking for the equivalent of the matlab command: axis ij;
Also, I've spent a couple hours surfing matplotlib help and google but haven't come up with an answer. Some info on where I could have looked up the answer would be helpful as well.
The easiest way is to use:
plt.gca().invert_yaxis()
After you plotted the image. Origin works only for imshow.
axis ij just makes the y-axis increase downward instead of upward, right? If so, then matplotlib.axes.invert_yaxis() might be all you need -- but I can't test that right now.
If that doesn't work, I found a mailing post suggesting that
setp(gca(), 'ylim', reversed(getp(gca(), 'ylim')))
might do what you want to resemble axis ij.
For an image or contour plot, you can use the keyword origin = None | 'lower' | 'upper' and for a line plot, you can set the ylimits high to low.
from pylab import *
A = arange(25)/25.
A = A.reshape((5,5))
figure()
imshow(A, interpolation='nearest', origin='lower')
figure()
imshow(A, interpolation='nearest')
d = arange(5)
figure()
plot(d)
ylim(5, 0)
show()
The following is a basic way to achieve this
ax=pylab.gca()
ax.set_ylim(ax.get_ylim()[::-1])
This
plt.ylim(max(plt.ylim()), min(plt.ylim()))
has an advantage over this
plt.gca().invert_yaxis()
and is that if you are in interactive mode and you repeatedly plot the same plot (maybe with updated data and having a breakpoint after the plot) the y axis won't keep inverting every time.
I'm plotting some data with matplotlib. I want the plot to focus on a specific range of x-values, so I'm using set_xlim().
Roughly, my code looks like this:
fig=plt.figure()
ax=fig.add_subplot(111)
for ydata in ydatalist:
ax.plot(x_data,y_data[0],label=ydata[1])
ax.set_xlim(left=0.0,right=1000)
plt.savefig(filename)
When I look at the plot, the x range ends up being from 0 to 12000. This occurs whether set_xlim() occurs before or after plot(). Why is set_xlim() not working in this situation?
Out of curiosity, what about switching in the old xmin and xmax?
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xlim(xmin=0.0, xmax=1000)
plt.savefig(filename)
The text of this answer was taken from an answer that was deleted almost immediately after it was posted.
set_xlim() limits the data that is displayed on the plot.
In order to change the bounds of the axis, use set_xbound().
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x_data,y_data)
ax.set_xbound(lower=0.0, upper=1000)
plt.savefig(filename)
In my case the following solutions alone did not work:
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
However, setting the aspect using set_aspect worked, i.e:
ax.set_aspect('auto')
ax.set_xlim([0, 5.00])
ax.set_xbound(lower=0.0, upper=5.00)
I have struggled a lot with the ax.set_xlim() and couldn't get it to work properly and I found out why exactly. After setting the xlim I was setting the xticks and xticklabels (those are the vertical lines on the x-axis and their labels) and this somehow elongated the axis to the needed extent. So if the last tick was at 300 and my xlim was set at 100, it again widened the axis to the 300 just to place the tick there.
So the solution was to put it just after the troublesome code:
ax.set_xlabel(label)
ax.set_xticks(xticks)
ax.set_xticklabels(xticks, rotation=60)
ax.set_xlim(xmin=0.0, xmax=100.0)
The same thing occurred to me today. My issue was that the data was not in the right format, i.e. not floats. The limits I set (itself floats) became meaningless compared to e.g. strings. After putting float() around the data, everything worked as expected.
How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).
I'm looking for the equivalent of the matlab command: axis ij;
Also, I've spent a couple hours surfing matplotlib help and google but haven't come up with an answer. Some info on where I could have looked up the answer would be helpful as well.
The easiest way is to use:
plt.gca().invert_yaxis()
After you plotted the image. Origin works only for imshow.
axis ij just makes the y-axis increase downward instead of upward, right? If so, then matplotlib.axes.invert_yaxis() might be all you need -- but I can't test that right now.
If that doesn't work, I found a mailing post suggesting that
setp(gca(), 'ylim', reversed(getp(gca(), 'ylim')))
might do what you want to resemble axis ij.
For an image or contour plot, you can use the keyword origin = None | 'lower' | 'upper' and for a line plot, you can set the ylimits high to low.
from pylab import *
A = arange(25)/25.
A = A.reshape((5,5))
figure()
imshow(A, interpolation='nearest', origin='lower')
figure()
imshow(A, interpolation='nearest')
d = arange(5)
figure()
plot(d)
ylim(5, 0)
show()
The following is a basic way to achieve this
ax=pylab.gca()
ax.set_ylim(ax.get_ylim()[::-1])
This
plt.ylim(max(plt.ylim()), min(plt.ylim()))
has an advantage over this
plt.gca().invert_yaxis()
and is that if you are in interactive mode and you repeatedly plot the same plot (maybe with updated data and having a breakpoint after the plot) the y axis won't keep inverting every time.