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!
Related
Matplotlib has some pretty sophisticated code figuring out how to show labels, but sometimes it cramps its labels more than looks good on presentations. Is there any way to tweek it?
For example, suppose we're plotting something against date:
figure = plt.figure(figsize=(8,1))
ax = plt.gca()
ax.set_xlim(xmin=np.datetime64('2010'), xmax=np.datetime64('2020-04-01'))
We get an x-axis like this:
But supposing we want it to show more spaced years, like this:
We can kludge it in any given case by editing the labels 'mechanically'. E.g.:
ax.set_xticks([tick for i, tick in enumerate(ax.get_xticks()) if i%2==0]) # Every other year.
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))
But that's fragile, and it breaks whenever the x limits change.
Is there any way to force more spacing in the tick setup algorithm?
Oh! Found the matplotlib source code and it led me to AutoDateLocator:
ax.xaxis.set_major_locator(matplotlib.dates.AutoDateLocator(maxticks=8))
The corresponding locator for non-dates is MaxNLocator .
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.
I have the following code:
fig, ax = plt.subplots(1, 1)
calls["2016-12-24"].resample("1h").sum().plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().plot(ax=ax)
which generates the following image:
How can I make this so the lines share the x-axis? In other words, how do I make them not switch days?
If you don't care about using the correct datetime as index, you could just reset the index as you suggested for all the series. This is going to overlap all the time series, if this is what you're trying to achieve.
# the below should
calls["2016-12-24"].resample("1h").sum().reset_index("2016-12-24").plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().reset_index("2016-12-25").plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().reset_index("2016-12-26").plot(ax=ax)
Otherwise you should try as well to resample the three columns at the same time. Have a go with the below but not knowing how your original dataframe look like, I'm not sure this will fit your case. You should post some more information about the input dataframe.
# have a try with the below
calls[["2016-12-24","2016-12-25","2016-12-26"].resample('1h').sum().plot()
I am plotting a timeserie with matplotlib (timeserie looks like the following):
Part of the code that i use sets major locator for each day at 0AM:
fig, ax = plt.subplots(figsize=(20, 3))
mpf.candlestick_ohlc(ax,quotes, width=0.01)
ax.xaxis_date()
ax.xaxis.set_major_locator(mpl.dates.DayLocator(interval=1) )
I would like to plot a darker background on the chart for each day between 16pm and 8am, and planning to use axvspan for that task. Considering that axvspan takes as argument axvspan(xmin, xmax) I was wondering if it would be possible to retrieve the xaxis_major_locator as a x value in order to pass it to axvspan as axvspan(xmin=major_locator-3600s, xmax=major_locator+3600s)
Edit: I found that function in the docs: http://matplotlib.org/2.0.0rc2/api/ticker_api.html#matplotlib.ticker.Locator
If anyone knows how to returns a list of ticker location from the Xaxis_major with it let me know. Thanks.
Edit2: if i use print(ax.xaxis.get_major_locator()) i receive as a return <matplotlib.dates.DayLocator object at 0x7f70f3b34090> How do i extarct a list of tick location from that?
ok found it...
majors=ax.xaxis.get_majorticklocs()
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