Pandas show multiple histograms in a chart
my data looks like this:
term date change1 change2
aaa 2010-03-01 23.00 24.31
bbb 2010-03-01 25.00 0.00
ccc 2012-05-01 100.00 100.00
The date column can have duplicate dates. I want to build for each term what change1 and change2 are. I thought the term was as x-axis, and change1 and change2 have the same y-axis, but will display as bar graphs side by side. I know how to make a part of the y-axis, but I don't know how to set the x-axis. I would also like each term to show the date in some way if possible, otherwise it is not a priority.
Here's what I have:
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df.change1.plot(kind = 'bar', color = 'red', ax = ax , position = 1)
df.change2.plot(kind = 'bar', color = 'blue', ax = ax2, position = 2)
ax.set_ylabel= ('change1')
ax2.set_ylabel=('change2')
plt.show()
Thank,
source to share
One way to make the labels x-axis
equal term
is to set term
as an index:
df = df.set_index(['term'])
For example,
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
df = df.set_index(['term'])
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
df['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = ('change1')
ax2.set_ylabel = ('change2')
plt.show()
Or, instead of setting the index, you can explicitly set the xticklabels symbols:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
fig = plt.figure()
ax = fig.add_subplot(111)
ax2 = ax.twinx()
df['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
df['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = 'change1'
ax2.set_ylabel = 'change2'
labels = ['{}\n{}'.format(date, term) for date, term in zip(df['date'], df['term'])]
ax.set_xticklabels(labels, minor=False)
fig.autofmt_xdate()
plt.show()
In the question in the comments, to create a new plot for each date
, you can iterate over the groups in
df.groupby(['date'])
:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'change1': [23.0, 25.0, 100.0],
'change2': [24.309999999999999, 0.0, 100.0],
'date': ['2010-03-01', '2010-03-01', '2012-05-01'],
'term': ['aaa', 'bbb', 'ccc']})
groups = df.groupby(['date'])
fig, axs = plt.subplots(nrows=groups.ngroups)
for groupi, ax in zip(groups,axs):
index, grp = groupi
ax2 = ax.twinx()
grp['change1'].plot(kind='bar', color='red', ax=ax, position=0, width=0.25)
grp['change2'].plot(kind='bar', color='blue', ax=ax2, position=1, width=0.25)
ax.set_ylabel = 'change1'
ax2.set_ylabel = 'change2'
ax.set_title(index)
ax.set_xticklabels(grp['term'].tolist(), minor=False, rotation=0)
fig.tight_layout()
plt.show()
source to share