[英]Subplot of Subplots Matplotlib / Seaborn
我正在嘗試創建子圖網格。 每個子圖看起來都像該站點上的圖。
https://python-graph-gallery.com/24-histogram-with-a-boxplot-on-top-seaborn/
如果我有10套不同的這種情節圖,我想將它們設置為5x2。
我已經閱讀了Matplotlib的文檔,似乎無法弄清楚該怎么做。 我可以循環子圖並獲取每個輸出,但無法將其放入行和列
以pd格式導入熊貓,以np格式導入numpy,以sns格式導入seaborn
df = pd.DataFrame(np.random.randint(0,100,size=(100, 10)),columns=list('ABCDEFGHIJ'))
for c in df :
# Cut the window in 2 parts
f, (ax_box,
ax_hist) = plt.subplots(2,
sharex=True,
gridspec_kw={"height_ratios":(.15, .85)},
figsize = (10, 10))
# Add a graph in each part
sns.boxplot(df[c], ax=ax_box)
ax_hist.hist(df[c])
# Remove x axis name for the boxplot
plt.show()
結果將采用此循環並將它們放到一組行和列中,在這種情況下為5x2
您有10列,每列創建2個子圖:箱形圖和直方圖。 因此,您總共需要20個數字。 您可以通過創建2行10列的網格來做到這一點
完整答案:(根據口味調整figsize
和height_ratios
)
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
f, axes = plt.subplots(2, 10, sharex=True, gridspec_kw={"height_ratios":(.35, .35)},
figsize = (12, 5))
df = pd.DataFrame(np.random.randint(0,100,size=(100, 10)),columns=list('ABCDEFGHIJ'))
for i, c in enumerate(df):
sns.boxplot(df[c], ax=axes[0,i])
axes[1,i].hist(df[c])
plt.tight_layout()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.