Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is matplotlib .plot(kind='bar') plot so different to .plot()

This may be a very stupid question, but when plotting a Pandas DataFrame using .plot() it is very quick and produces a graph with an appropriate index. As soon as I try to change this to a bar chart, it just seems to lose all formatting and the index goes wild. Why is this the case? And is there an easy way to just plot a bar chart with the same format as the line chart?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame()
df['Date'] = pd.date_range(start='01/01/2012', end='31/12/2018')
df['Value'] = np.random.randint(low=5, high=100, size=len(df))
df.set_index('Date', inplace=True)

df.plot()
plt.show()

df.plot(kind='bar')
plt.show()

df.plot()

df.plot(kind='bar'

Update: For comparison, if I take the data and put it into Excel, then create a line plot and a bar ('column') plot it instantly will convert the plot and keep the axis labels as they were for the line plot. If I try to produce many (thousands) of bar charts in Python with years of daily data, this takes a long time. Is there just an equivalent way of doing this Excel transformation in Python?

Excel plots

like image 494
13sen1 Avatar asked Mar 21 '26 21:03

13sen1


1 Answers

Pandas bar plots are categorical in nature; i.e. each bar is a separate category and those get their own label. Plotting numeric bar plots (in the same manner a line plots) is not currently possible with pandas.

In contrast matplotlib bar plots are numerical if the input data is numbers or dates. So

plt.bar(df.index, df["Value"])

produces

enter image description here

Note however that due to the fact that there are 2557 data points in your dataframe, distributed over only some hundreds of pixels, not all bars are actually plotted. Inversely spoken, if you want each bar to be shown, it needs to be one pixel wide in the final image. This means with 5% margins on each side your figure needs to be more than 2800 pixels wide, or a vector format.

So rather than showing daily data, maybe it makes sense to aggregate to monthly or quarterly data first.

like image 112
ImportanceOfBeingErnest Avatar answered Mar 24 '26 11:03

ImportanceOfBeingErnest



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!