Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib adding too many labels to bar chart [duplicate]

I'm having a slightly frustrating situation with pandas/matplotlib (not sure which one's at fault here). If I create a simple random series and plot it as a line chart, the formatting is pretty clean:

test1 = pd.Series(index = pd.date_range(start='2000-06-30', end='2018-06-30', freq='3M'),
                 data = np.random.rand(73))
test1.plot(kind='line')

Most satisfactory. If I change the format to bar, though, the x-axis formatting goes to hell in a handbasket:

enter image description here

Matplotlib seems to feel obliged to label every single bar. Is there a logical reason for this or an easy fix?

Thanks in advance.

like image 387
Chris Harris Avatar asked Oct 14 '25 14:10

Chris Harris


1 Answers

Matplotlib is trying to use each time as its own bar, and is trying to label each bar appropriately. To change this behaviour, you should change the xticks or the xticklabels. For example, one quick way to just remove the labels entirely is to do something like

subplot = test1.plot(kind='bar')
ax = subplot.axes
#ax.set_xticks([])       # Alternatively, you can manually adjust the ticks
ax.set_xticklabels([])   # or their labels
f = ax.get_figure()
f.show()

which will produce

A plot with no tick labels

You can reduce the number of ticks to something like using every nth tick and its label in the same way.

like image 53
davidlowryduda Avatar answered Oct 17 '25 06:10

davidlowryduda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!