0

我正在尝试使用并行循环创建一些内核图。当我使用条形图时循环工作,但当我使用内核图时会出错。我是 python 新手,所以我认为我遗漏了一些非常明显的东西 - 有什么建议吗?谢谢!

哦和len(schools) = 3

#the kernel plot
fig = plt.figure(facecolor='white')
gs1 = GridSpec(1,len(schools))
sp1 = [plt.subplot(gs1[0,i]) for i in range(len(schools))]
colors = ["red", "blue", "green"]
schools2 = [[data1....],[data2....],[data3......]]
for ax, i in zip(sp1, range(len(schools))):
    ax = sns.kdeplot(schools2[i], bw=.5, color = colors[i], lw=1.8, vertical=True, alpha=.5)

.

#the bar plot
fig = plt.figure(facecolor='white')
gs1 = GridSpec(1,len(schools))
sp1 = [plt.subplot(gs1[0,i]) for i in range(len(schools))]
colors = ["red", "blue", "green"]
test = [1,2,3]
for ax, i in zip(sp1, range(3)):
    ax.bar(1, test[i], color = colors[i])
4

1 回答 1

1

对于直接使用 matplotlib 绘图函数,做,例如plt.bar(...)ax.bar(...). 在前一种情况下,绘图将绘制在“当前活动”轴上,而后一种情况下,绘图将始终位于与ax变量绑定的轴上。

类似地,如果您只是编写 seaborn 绘图功能,例如sns.kdeplot(...),它将在“当前活动”轴上绘图。为了使用 matplotlib 面向对象的界面来控制绘图的最终位置,大多数 [1] seaborn 函数采用一个ax参数,您将 Axes 对象传递给该参数:sns.kdeplot(..., ax=ax).

  1. kdeplot我说的最多,因为像,violinplot和许多其他绘制到特定轴上的函数和更复杂的函数(如lmplot,等)之间存在区别factorplot,它们是全图函数,不能分配给特定的轴或图形. 任何以前的函数都会接受一个ax参数。
于 2014-05-22T20:13:35.923 回答