2016-06-13 18 views
1

Ich habe Grundstücke wie diesesWie Grundstück hinzufügen matplotlib

fig = plt.figure() 
desire_salary = (df[(df['inc'] <= int(salary_people))]) 
print desire_salary 
# Create the pivot_table 
result = desire_salary.pivot_table('city', 'cult', aggfunc='count') 

# plot it in a separate step. this returns the matplotlib axes 
ax = result.plot(kind='bar', alpha=0.75, rot=0, label="Presence/Absence of cultural centre") 

ax.set_xlabel("Cultural centre") 
ax.set_ylabel("Frequency") 
ax.set_title('The relationship between the wage level and the presence of the cultural center') 
plt.show() 

ich das subplot hinzufügen möchten Nebenhandlung. Ich versuche

fig, ax = plt.subplots(2, 3) 
... 
ax = result.add_subplot() 

aber es gibt Attribute: 'Series' Objekt hat kein Attribut ‚add_subplot'`. Wie kann ich diesen Fehler überprüfen?

+0

tun Sie 6 Plots plotten möchten? – MaxU

+0

@MaxU, Ja, ich möchte 6 Plot zu einem hinzufügen. Ich habe das separat, aber ich will das vereinen –

+0

überprüfen Sie den Link, ich habe in meiner Antwort zur Verfügung gestellt - ich war dort fast das gleiche (ich war mit seaborn.boxplot statt Barplot, die Sie verwenden möchten) – MaxU

Antwort

3

matplotlib.pyplot hat das Konzept der aktuellen Zahl verwenden und die aktuelle Achsen. Alle Zeichenbefehle gelten für die aktuellen Achsen.

import matplotlib.pyplot as plt 

fig, axarr = plt.subplots(2, 3)  # 6 axes, returned as a 2-d array 

#1 The first subplot 
plt.sca(axarr[0, 0])    # set the current axes instance to the top left 
# plot your data 
result.plot(kind='bar', alpha=0.75, rot=0, label="Presence/Absence of cultural centre") 

#2 The second subplot 
plt.sca(axarr[0, 1])    # set the current axes instance 
# plot your data 

#3 The third subplot 
plt.sca(axarr[0, 2])    # set the current axes instance 
# plot your data 

Demo:

enter image description here

Der Quellcode,

import matplotlib.pyplot as plt 
fig, axarr = plt.subplots(2, 3, sharex=True, sharey=True)  # 6 axes, returned as a 2-d array 

for i in range(2): 
    for j in range(3): 
     plt.sca(axarr[i, j])      # set the current axes instance 
     axarr[i, j].plot(i, j, 'ro', markersize=10) # plot 
     axarr[i, j].set_xlabel(str(tuple([i, j]))) # set x label 
     axarr[i, j].get_xaxis().set_ticks([])  # hidden x axis text 
     axarr[i, j].get_yaxis().set_ticks([])  # hidden y axis text 

plt.show() 
+0

Ich habe einen Plot in 'result' und ich möchte ihn zur Liste mit 6 Plots hinzufügen –

+0

@ArseniyKrupenin, benutze' plt.sca', um die aktuelle Achseninstanz und dann zu setzen Plotten Sie Ihre Daten. Bitte überprüfen Sie die aktualisierte Antwort. – SparkAndShine

+0

es gibt 'AttributeError: 'NoneType' Objekt hat kein Attribut 'set_xlabel'' –

0

result ist der pandas.Series-Typ, der keine add_subplot() Methode hat.

Verwendung fig.add_subplot(...) statt

Hier ist ein example (mit Seaborn Modul):

labels = df.columns.values 
fig, axes = plt.subplots(nrows = 3, ncols = 4, gridspec_kw = dict(hspace=0.3),figsize=(12,9), sharex = True, sharey=True) 
targets = zip(labels, axes.flatten()) 
for i, (col,ax) in enumerate(targets): 
    sns.boxplot(data=df, ax=ax, color='green', x=df.index.month, y=col) 

Sie können Pandas Plots statt seaborn

+0

Wie kann ich dieses Plot, das in 'result 'enthalten ist, zum Subplot hinzufügen? –

+0

wenn ich das benutze gibt es 'AttributeError: 'NoneType' Objekt hat kein Attribut 'set_xlabel'' –

+0

Ich sollte dies tun mit' matplotlib' oder 'pandas' –

Verwandte Themen