Python 如何在特定轴上使用熊猫中的DataFrame.hist(by=)绘图

Python 如何在特定轴上使用熊猫中的DataFrame.hist(by=)绘图,python,matplotlib,pandas,Python,Matplotlib,Pandas,我尝试在同一个图中绘制多个直方图组。每个组包含两个条件,因此我使用pandas histogram options中的“by=”参数。然而,这并没有像我预期的那样起作用,熊猫创建了一个新的图形,而不是在我经过的轴上绘制。我也试着通过四个轴,但仍然没有通过。示例代码: import pandas as pd df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]}) fig,

我尝试在同一个图中绘制多个直方图组。每个组包含两个条件,因此我使用pandas histogram options中的“by=”参数。然而,这并没有像我预期的那样起作用,熊猫创建了一个新的图形,而不是在我经过的轴上绘制。我也试着通过四个轴,但仍然没有通过。示例代码:

import pandas as pd
df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]})
fig, (ax1, ax2) = plt.subplots(1,2)
df.area.hist(by=df.color, ax=ax1)

我正在使用pandas 0.12.0、matplotlib 1.3.0和python 2.7.5。欢迎提出在同一子地块网格中组合/缝合多个“hist(by=)-plots”的建议

更新:

也许这更准确地描述了我想要实现的目标

import pandas as pd
df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]})
#fig, (ax1, ax2) = plt.subplots(1,2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
ax3.plot([[2,2], [3,6]])
ax4.plot([[3,6], [2,2]])
df.area.hist(by=df.color, ax=ax1)
理想情况下,在我的示例中,pandas直方图为1,2,然后应将ax1拆分为两个子批次。或者,可以将其绘制到ax1和ax2中,然后用户可以确保有正确数量的空子批次可用。

这是固定的,因为从那时起已合并到0.15.0中

将多个绘图传递到现有地物的正确方法是,首先创建所有所需的轴,然后将所有轴传递给熊猫绘图命令

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'color': ['blue','blue','yellow','blue','yellow'], 'area': [2,2,3,4,4]})
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
#Hide upper left corner plot and create two new subplots
ax1.axis('off')
ax = fig.add_subplot(2,4,1)
ax0 = fig.add_subplot(2,4,2)
#Plot
ax3.plot([[2,2], [3,6]])
ax4.plot([[3,6], [2,2]])
df.area.hist(by=df.color, ax=(ax,ax0)) #Pass both new subplots


您可以使用

您正在使用的pandas版本更优雅地创建子地块?我看到pandas v 0.10.0也有同样的行为。在创建HistorogramPandas 0.12.0、matplotlib 1.3.0和python 2.7.5的过程中,变量ax似乎丢失了。还为问题添加了版本号。如果您不告诉我们
by=
直方图是否绘制在正确的轴上?我觉得它像一个bug。或者用
by=
记录行为。你最好在网上提交一个问题,看看他们怎么说。