首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子图问题:如何根据分类值为每个图绘制直方图?

子图问题:如何根据分类值为每个图绘制直方图?
EN

Stack Overflow用户
提问于 2019-02-06 03:22:37
回答 2查看 158关注 0票数 0

我有一个DataFrame,它有三个数值变量,孔隙率,Perm和AI。我想做一个子图,在每个图中,我想要三个变量的直方图,通过一个分类变量‘相’。“相”只能接受两个值:“砂”和“页”。

总而言之,每个子图都需要一个直方图,并且每个直方图必须基于分类变量相绘制,以便在相之间进行比较。

到目前为止,我可以让它工作,但我不能将轴标题添加到每个子图中。

代码语言:javascript
复制
plt.subplot(311)
plt.hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
plt.hist(df_sh['Porosity'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='Porosity (fraction)', ylabel='Density', title='Porosity              
      Histogram')
plt.legend()

plt.subplot(312)
plt.hist(df_sd['log10Perm'].values, label='Sand', bins=30, alpha=0.6,)
plt.hist(df_sh['log10Perm'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='Permeability (mD)', ylabel='Density', title='Permeability 
Histogram')
plt.legend()

plt.subplot(313)
plt.hist(df_sd['AI'].values, label='Sand', bins=30, alpha=0.6)
plt.hist(df_sh['AI'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='AI (units)', ylabel='Density', title='Acoustic Impedance 
Histogram')

plt.legend()
plt.subplots_adjust(left=0.0, bottom=0.0, right=1.5, top=3.5, wspace=0.1, 
hspace=0.2);


#I have tried with:
fig, axs = plt.subplots(2, 1)
but when I code
axs[0].hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
axs[0].hist(df_sd['Porosity'].values, label='Shale', bins=30, alpha=0.6)

#But the histogram for shale overrides the histogram for Sand.

I would like to have this result,但同时具有x和y轴以及标签名称。此外,为每个子图都有一个标题会很有帮助。

EN

回答 2

Stack Overflow用户

发布于 2019-02-06 03:31:44

我刚刚做了一个等高线的子图,但我认为框架将非常相似:

代码语言:javascript
复制
fig, axs = plt.subplots(2, 2, constrained_layout=True)
for ax, extend in zip(axs.ravel(), extends):
    cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
    fig.colorbar(cs, ax=ax, shrink=0.9)
    ax.set_title("extend = %s" % extend)
    ax.locator_params(nbins=4)
plt.show()

我认为要注意的要点(这是我从下面的链接中了解到的)是他们在for循环中使用zip(axs.ravel())来建立每个ax,然后在该ax上绘制您想要的内容。我非常确定您可以根据自己的用途对其进行调整。

完整示例可在以下网址获得:https://matplotlib.org/gallery/images_contours_and_fields/contourf_demo.html#sphx-glr-gallery-images-contours-and-fields-contourf-demo-py

票数 0
EN

Stack Overflow用户

发布于 2019-02-06 04:16:31

我找到了答案:

代码语言:javascript
复制
fig = plt.figure()
ax = fig.add_subplot(111)
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax2 = fig.add_subplot(313)


plt.subplot(311)
ax1.hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
ax1.hist(df_sh['Porosity'].values, label='Shale', bins=30, alpha=0.6)
ax1.set(xlabel='Porosity (fraction)', ylabel='Density', title='Porosity Histogram')
ax1.legend()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54541625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档