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? Thanks!
oh and 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(...)
. 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.
Analogously, with seaborn plotting functions if you just write, eg sns.kdeplot(...)
, it will plot onto the "currently active" axes. 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)
.
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. Any of the former functions will take an ax
argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.