pandas 如何将图添加到子图 matplotlib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37798645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How add plot to subplot matplotlib
提问by Arseniy Krupenin
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
我想将此添加到subplot
. 我试试
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?
但它返回 AttributeError: 'Series' object has no attribute 'add_subplot'`。我怎样才能检查这个错误?
回答by SparkAndShine
matplotlib.pyplot
has the concept of the current figure and the current axes. All plotting commands apply to the current axes.
matplotlib.pyplot
具有当前图形和当前轴的概念。所有绘图命令都适用于当前轴。
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:
演示:
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()
回答by MaxU
result
is of pandas.Series type, which doesn't have add_subplot()
method.
result
是 pandas.Series 类型,它没有add_subplot()
方法。
use fig.add_subplot(...)
instead
使用fig.add_subplot(...)
替代
Here is an example(using seaborn module):
这是一个示例(使用 seaborn 模块):
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
您可以使用Pandas图而不是 seaborn