简体   繁体   English

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

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

I'm trying to create a handful of kernel plots using a parallel loop. 我正在尝试使用并行循环创建一些内核图。 The loop works when I use a bar chart, but goes awry when I use a kernel plot. 使用条形图时,该循环有效,但使用内核图时,该循环出错。 I'm new to python so I assume I'm missing something pretty obvious - any suggestions? 我是python的新手,所以我想我想念一些明显的东西-有什么建议吗? Thanks! 谢谢!

oh and len(schools) = 3 哦, 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])

For using matplotlib plotting functions directly, there's a difference between doing, eg plt.bar(...) and ax.bar(...) . 对于直接使用matplotlib绘图函数,这样做之间有区别,例如plt.bar(...)ax.bar(...) In the former case, the plot will be drawn on the "currently active" axes, while the latter case the plot will always go onto the Axes bound to the ax variable. 在前一种情况下,该图将绘制在“当前活动”轴上,而在后一种情况下,该图将始终绘制在绑定到ax变量的轴上。

Analogously, with seaborn plotting functions if you just write, eg sns.kdeplot(...) , it will plot onto the "currently active" axes. 类似地,使用seaborn绘图功能(如果您只是编写),例如sns.kdeplot(...) ,它将绘制在“当前活动”轴上。 To control where the plot will end up using the matplotlib object-oriented interface, most[1] seaborn functions take an ax parameter, to which you pass the Axes object: sns.kdeplot(..., ax=ax) . 要使用matplotlib面向对象的界面控制绘图的最终位置,大多数[1] seaborn函数都采用ax参数,您将Axes对象传递给该参数: sns.kdeplot(..., ax=ax)

  1. I say most as there is a distinction between functions like kdeplot , violinplot , and many others that plot onto a specific Axes, and more complex functions like lmplot , factorplot , etc. that are whole-figure functions and can't be assigned to a specific Axes or Figure. 我之所以这么说是因为kdeplotviolinplot和其他许多绘制在特定violinplot函数之间的区别,以及lmplotfactorplot等更复杂的函数lmplot factorplot ,这些函数是全图函数,不能分配给a特定的轴或图。 Any of the former functions will take an ax argument. 以前的任何函数都将使用ax参数。

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

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