1

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])
3
  • 1
    Try calling sns.kdeplot with sns.kdeplot(..., ax=ax) to control where the plot ends up. Commented May 22, 2014 at 18:45
  • @mwaskom thanks so much! did you want to post that as answer so I can select it appropriately? Commented May 22, 2014 at 18:51
  • By the way, a simpler way to set up the plot would be fig, axes = plt.subplots(1, 3) Commented May 24, 2014 at 5:58

1 Answer 1

1

For using matplotlib plotting functions directly, there's a difference between doing, e.g. 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, e.g. 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).

  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. Any of the former functions will take an ax argument.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.