[英]Plotting complicated histograms side-by-side
I am trying to plot three histograms side-by-side (one row, 3 columns) by using a for loop and iterating over the same code but for a different column.我正在尝试通过使用 for 循环并迭代相同的代码但不同的列来并排 plot 三个直方图(一行,三列)。 I found two different methods, one is to use the plt.subplots(1, 3, i) and then iterate over i, and the other is to explicitly set fig, ax = plt.subplots() and replace each plt with ax[i] and iterate over that.我找到了两种不同的方法,一种是使用 plt.subplots(1, 3, i) 然后迭代 i,另一种是显式设置 fig, ax = plt.subplots() 并将每个 plt 替换为 ax[ i] 并对其进行迭代。
However neither of these works properly because of the additional lines I plot on the histogram(s).但是,由于直方图上的附加行 I plot ,这些都不能正常工作。
How could I make this code work for side-by-side plots?我怎样才能让这段代码适用于并排的情节?
for parameter in ['k', 'm', 'sig']:
plt.hist(df[parameter], density=True, bins=20, label=parameter, ec='black')
mn, mx = plt.xlim()
plt.xlim(mn, mx)
kde_xs = np.linspace(mn, mx, 301)
kde = st.gaussian_kde(df[parameter])
plt.plot(kde_xs, kde.pdf(kde_xs), label="PDF")
plt.legend(loc="upper left")
plt.ylabel('Frequency')
plt.xlabel([parameter])
plt.title(f"Histogram for bootstrapped: {parameter}")
plt.axvline(df[parameter].mean(), color='r', linestyle='solid',
linewidth=2)
I just had to do the below.我只需要执行以下操作。 Use the axes[i] on everything and the zip function to iterate over histograms.使用轴[i] 和 zip function 对直方图进行迭代。
fig, axes = plt.subplots(1, 3, figsize=(26, 7))
for parameter, i in zip(['k', 'm', 'sig'], [0, 1, 2]):
axes[i].hist(data[parameter].dropna(), density=True, bins=20, label=parameter, ec='black')
mn, mx = axes[i].set_xlim()
axes[i].set_xlim(mn, mx)
kde_xs = np.linspace(mn, mx, 301)
kde = st.gaussian_kde(data[parameter].dropna())
axes[i].plot(kde_xs, kde.pdf(kde_xs), label="PDF")
axes[i].legend(loc="upper left")
axes[i].set_ylabel('Frequency')
axes[i].set_xlabel([parameter])
axes[i].set_title(f"Histogram for bootstrapped: {parameter}")
axes[i].axvline(data[parameter].dropna().mean(), color='r', linestyle='solid', linewidth=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.