2016-06-29 5 views
1

ich Pandas Datenrahmen wie diese bekommen haben:Doppel Graph mit matplotlib: Attribut Fehler

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 
... 

Ich möchte eine Doppelfigur schaffen, die etwa wie folgt aussieht: figure

Der Code habe ich versucht, :

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) 

Aber ich bekomme diese Fehlermeldung: Attribute: ‚Dataframe‘ Objekt ‚gefunden kein Attribut

Wie soll ich die Figur erstellen, die ich erhalten möchte?

Antwort

1

Eine mögliche Lösung ist Series.plot.bar verwenden:

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() 

graph

Sie können auch Orientierung der Etiketten von Achse ändern 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() 

graph1

+0

Dank jezrael! Leider bekomme ich die Fehlermeldung: TypeError: Empty 'DataFrame': keine numerischen Daten zum Plotten. Hast du eine Idee, wie du das loswerden kannst? – Papie

+0

Was ist 'print df.dtypes'? – jezrael

+0

Der dtype ist ein Objekt. Ist es in Ihrem Fall ein Integer/Float? – Papie

Verwandte Themen