Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have plots like this

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

I want to add this to subplot. I try

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

but it returns AttributeError: 'Series' object has no attribute 'add_subplot'`. How can I check this error?

share|improve this question
    
do you want to plot 6 plots? – MaxU Jun 13 at 20:57
    
@MaxU, Yes, I want to add 6 plot to one. I have that separately, but I want to unite that – Arseniy Krupenin Jun 13 at 20:59
    
check the link, i've provided in my answer - i was doing there almost the same (i was using seaborn.boxplot instead of barplot, that you want to use) – MaxU Jun 13 at 21:01
1  
Try always to provide a Minimal, Complete, and Verifiable example when asking questions. In case of pandas questions please provide sample input and output data sets (5-7 rows in CSV/dict/JSON/Python code format as text, so one could use it when coding an answer for you). This will help to avoid situations like: your code isn't working for me or it doesn't work with my data, etc. – MaxU Jun 13 at 21:05
up vote 1 down vote accepted

matplotlib.pyplot has the concept of the current figure and the current axes. All plotting commands apply to the current axes.

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

The source code,

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()
share|improve this answer
    
I have plot in result and I want add it to list with 6 plots – Arseniy Krupenin Jun 13 at 21:03
    
@ArseniyKrupenin, use plt.sca to set the current axes instance and then plot your data. Pls check the updated answer. – sparkandshine Jun 13 at 21:07
    
it returns AttributeError: 'NoneType' object has no attribute 'set_xlabel' – Arseniy Krupenin Jun 13 at 21:12
    
@ArseniyKrupenin, use axarr[0, 0].set_xlabel – sparkandshine Jun 13 at 21:14
    
when I use ax = it doesn't add plot to graph. I have only labels, but graph is empty – Arseniy Krupenin Jun 13 at 21:25

result is of pandas.Series type, which doesn't have add_subplot() method.

use fig.add_subplot(...) instead

Here is an example (using seaborn module):

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)

You can use pandas plots instead of seaborn

share|improve this answer
    
How can I add this plot that contain in result to subplot? – Arseniy Krupenin Jun 13 at 20:46
    
when I use that it return AttributeError: 'NoneType' object has no attribute 'set_xlabel' – Arseniy Krupenin Jun 13 at 20:55
    
I should to do this using matplotlib or pandas – Arseniy Krupenin Jun 13 at 21:28
    
@ArseniyKrupenin, well, then do it using matplotlib or pandas ;) – MaxU Jun 13 at 21:29
    
I don't understand, How can I add ax, that contain a plot to one of subplot – Arseniy Krupenin Jun 13 at 21:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.