Reputation: 989
I have got a pandas dataframe like this:
nan 0 ingredient contribution count
0 0.0 cracker 0.088844873 11
1 2.0 water 0.044386494 125
2 3.0 oil 0.034567456 10
3 4.0 flour 0.030855063 186
...
I would like to create a double figure that looks something like this:
The code I tried:
import matplotlib.pyplot as plt #importing libraries
import numpy as np
plt.figure(1) #creating empty figure
t = np.arange(0.0, 2.0, 0.01)
fig_contr = df[['ingredient','contribution']] #selecting columns
fig_count = df[['ingredient','count']]
plt.subplot(211) #creating subplot
plt.plot(t, fig_contr) #plotting subplot
plt.subplot(212)
plt.plot(t, fig_count)
But I get this error: AttributeError: 'DataFrame' object has no attribute 'find
How should I create the figure I would like to obtain?
Upvotes: 1
Views: 697
Reputation: 863166
One possible solution is use Series.plot.bar
:
plt.figure(1) #creating empty figure
df.set_index('ingredient', inplace=True) #set index from column ingredient
fig_contr = df['contribution'] #selecting columns
fig_count = df['count']
plt.subplot(211) #creating subplot
fig_contr.plot.bar() #plotting subplot
plt.subplot(212)
fig_count.plot.bar()
plt.show()
You can also change orientation of labels of axis x:
plt.figure(1) #creating empty figure
df.set_index('ingredient', inplace=True)
fig_contr = df['contribution'] #selecting columns
fig_count = df['count']
plt.subplot(211) #creating subplot
fig_contr.plot.bar(rot=0) #plotting subplot
plt.subplot(212)
fig_count.plot.bar(rot=0)
plt.show()
Upvotes: 1