繁体   English   中英

Matplotlib:创建具有并行循环和Seaborn的图

[英]Matplotlib: Creating plots with parallel loops & seaborn

我正在尝试使用并行循环创建一些内核图。 使用条形图时,该循环有效,但使用内核图时,该循环出错。 我是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])

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

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

  1. 我之所以这么说是因为kdeplotviolinplot和其他许多绘制在特定violinplot函数之间的区别,以及lmplotfactorplot等更复杂的函数lmplot factorplot ,这些函数是全图函数,不能分配给a特定的轴或图。 以前的任何函数都将使用ax参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM