我正在尝试创建一个子图的网格。每个子图看起来都像这个站点上的那个。
https://python-graph-gallery.com/24-histogram-with-a-boxplot-on-top-seaborn/
例如,如果我有10套不同的情节,我想把它们做成5x2。
我已经阅读了Matplotlib的文档,似乎无法理解如何做到这一点。我可以循环子图并获得每个输出,但不能将其放入行和列中。
进口熊猫为pd进口numpy为np进口海运为sns
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。
发布于 2019-05-26 19:48:37
您有10列,每个列创建两个子图:一个方框图和一个直方图。所以你总共需要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()

https://stackoverflow.com/questions/56316493
复制相似问题