gow*_*oww 5 python matplotlib histogram pandas subplot
我df.hist(columns=, by=)想在GridSpec(2,2)中插入4种不同的东西.
他们每个人都是这样的:
这是代码:
stuff = [df1, df2, df4, df3]
col = ['blue', 'orange', 'grey', 'green']
fig = plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(2, 2)
for i in range(0, len(stuff)):
ax = plt.subplot(gs[i])
stuff[i].hist(column='quanti_var', by=stuff[i].quali_var, alpha=.5, color=col[i], ax=ax)
Run Code Online (Sandbox Code Playgroud)
我有以下用户警告:
C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py:3234: UserWarning: To output multiple subplots, the figure containing the passed axes is being cleared
"is being cleared", UserWarning)
Run Code Online (Sandbox Code Playgroud)
而不是我要找的输出:
我尝试了几件事,包括使用SubplotSpec没有成功.任何的想法 ?
谢谢你们把你的神经元借给我!
解决方案是将matplotlib Axes返回的对象放置pd.DataFrame.hist()到具有所需布局的图形中。不幸的是,将新Axes对象放入现有对象中Figure有点复杂。
GridSpec布局使用嵌套创建您想要的布局并不太复杂matplotlib.gridspec.GridSpec(请参阅此处的示例)。像这样的东西。
import matplotlib.gridspec as gs
num_outer_columns = 2
num_outer_rows = 2
num_inner_columns = 2
num_inner_rows = 3
outer_layout = gs.GridSpec(num_outer_rows, num_outer_columns)
inner_layout = []
for row_num in range(num_outer_rows):
inner_layout.append([])
for col_num in range(num_outer_columns):
inner_layout[row_num].append(
gs.GridSpecFromSubplotSpec(
num_inner_rows,
num_inner_columns,
outer_layout[row_num, col_num]
)
)
Run Code Online (Sandbox Code Playgroud)
您可以使用ax = plt.subplot(inner_layout[outer_row_num][outer_col_num][inner_row_num, inner_col_num])并存储正确定位的axs 在此网格中创建子图以供以后使用。
Axes到现有的Figure您的df.hist()调用将产生如下内容:
In [1]: dframe.hist(column=x, by=y)
Out[1]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x1189be160>,
<matplotlib.axes._subplots.AxesSubplot object at 0x118e3eb50>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x118e74d60>,
<matplotlib.axes._subplots.AxesSubplot object at 0x118ea8340>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x118e76d62>,
<matplotlib.axes._subplots.AxesSubplot object at 0x118ea9350>]],
dtype=object)
Run Code Online (Sandbox Code Playgroud)
现在您只需将ax之前使用您定位的对象替换inner_layout为上面返回的对象即可AxesSubplot。不幸的是,没有一种方便的ax.from_axes(other_ax)方法可以做到这一点,因此您必须按照此答案Axes手动复制 over返回的结果。df.hist()
| 归档时间: |
|
| 查看次数: |
1487 次 |
| 最近记录: |