簡體   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