简体   繁体   English

matplotlib - 在具有重叠 x/y 刻度的相同轴上绘制两个直方图

[英]matplotlib - plotting two histograms in same axes with overlapping x/y ticks

I'm trying to plot histograms in multiple axes of my plot.我试图在我的情节的多个轴上绘制直方图。 My script below is doing that, but I simply can't standardize the x/y ticks for each ax ie with no overlapping, a common interval range for each ax.我下面的脚本正在这样做,但我根本无法标准化每个ax的 x/y 刻度,即没有重叠,每个轴的公共间隔范围。

I wanna specify the x/y ticks for each ax instead of setting global ticks for the whole plot.我想指定的x / y蜱每个ax ,而不是对整个情节设置全局蜱。

The output I have so far is below, too.到目前为止,我的输出也在下面。 As you can see the ticks are messed up, specially in the last ax .正如您所看到的,蜱虫被弄乱了,特别是在最后一个ax I've tried ax_set_x/yticks, plt.x/yticks.... but I'm pretty sure there is something I'm doing wrong.我试过 ax_set_x/yticks、plt.x/yticks .... 但我很确定我做错了什么。 Thanks a lot!!非常感谢!! 在此处输入图片说明

The script:剧本:

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss

data = np.random.normal(0.0005, 0.001,1000)
data2 = np.random.normal(0.0004, 0.001,1000)-data
diff = {k:[data, data2] for k in range(3)}
for k in diff:
    print k
ttls= ['SC2 vs SC1', 'SC4 vs SC3','SC6 vs SC5']
colors = ['chocolate', 'green']
leg = ['var1', 'var2'] 
bins = 100
fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(8.27, 11.69))
fig.add_subplot(111, frameon=False)
fig.suptitle('Normed Histograms', color='#6E6E6E', fontsize=10, fontweight='bold')
for ax, ttl, arr in zip(axes, ttls, diff.itervalues()):
    ax.hist((arr[0], arr[1]),bins, histtype='stepfilled', density=True, color=colors, label=leg, alpha=0.70)
    for i, v in enumerate(arr):
        mean = np.mean(v) 
        std = np.std(v)    
        xRange = np.linspace(np.min(v), np.max(v))#, len(cs))
        nd = ss.norm.pdf(xRange, mean, std)
        if  i == 0:
            socNd = ax.plot(xRange, nd, color='r', alpha=0.70, linewidth=0.8, label="fitted PDF*")
            socLine = ax.axvline(mean, color=colors[0], linestyle = ':',linewidth=2, label="Mean value")
        if i == 1:
            bcNd = ax.plot(xRange, nd, color='r', alpha=0.70, linewidth=0.8)
            bcLine = ax.axvline(mean, color=colors[1], linestyle = ':',linewidth=2)                 
    ax.set_title(ttl, loc='left', color='#6E6E6E', fontsize=8.5, fontweight='bold', pad=12)
    ax.set_ylabel("Frequency",  color='#6E6E6E', fontsize=8) 
    axes[2].legend(loc='lower center', bbox_to_anchor=(0.5, -0.5),ncol=4)
    axes[2].set_xlabel("xlabel", fontsize=8, labelpad=3)
plt.subplots_adjust(hspace=0.7)

plt.show()

You are creating you 3 subplots using fig, axes = plt.subplots(...) you are then creating another subplot on top of your existing 3. However because you have set frameon=False only the tick and tick labels are shown (try removing the frameon=False and you will see the problem).您正在使用fig, axes = plt.subplots(...)创建 3 fig, axes = plt.subplots(...)然后在现有 3 的顶部创建另一个子图。但是,因为您设置了frameon=False只显示刻度和刻度标签(尝试删除frameon=False ,您将看到问题)。

The solution is to simply remove fig.add_subplot(111, frameon=False) so that you are not creating an extra set of axes.解决方案是简单地删除fig.add_subplot(111, frameon=False)以便您不会创建额外的一组轴。

The resulting graph for me looks like:我的结果图如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM