【问题标题】:Empty bar chart matplotlib空条形图 matplotlib
【发布时间】:2020-06-25 00:45:18
【问题描述】:

我正在尝试使用以下两个数组创建条形图 - xbinmids 作为 x 坐标,xbinned_ms 作为条形的高度。

xbinned_ms:  [6.28e-16, 0, 0, 0, 0, 0, 6.28e-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.28e-16]

xbinmids:  [1.9869553456140592e-26, 5.960866036842178e-26, 9.934776728070296e-26, 1.3908687419298414e-25, 1.7882598110526532e-25, 2.185650880175465e-25, 2.583041949298277e-25, 2.9804330184210885e-25, 3.377824087543901e-25, 3.7752151566667126e-25, 4.172606225789524e-25, 4.569997294912336e-25, 4.967388364035148e-25, 5.36477943315796e-25, 5.762170502280771e-25, 6.159561571403584e-25, 6.556952640526395e-25, 6.954343709649207e-25, 7.351734778772019e-25, 7.749125847894831e-25, 8.146516917017643e-25, 8.543907986140454e-25, 8.941299055263266e-25, 9.338690124386079e-25, 9.73608119350889e-25, 1.0133472262631701e-24, 1.0530863331754513e-24, 1.0928254400877326e-24, 1.1325645470000137e-24, 1.1723036539122949e-24, 1.2120427608245762e-24, 1.2517818677368573e-24, 1.2915209746491384e-24, 1.3312600815614196e-24, 1.3709991884737009e-24, 1.410738295385982e-24, 1.4504774022982631e-24, 1.4902165092105445e-24, 1.5299556161228256e-24, 1.5696947230351067e-24, 1.6094338299473879e-24, 1.6491729368596692e-24, 1.6889120437719503e-24, 1.7286511506842314e-24, 1.7683902575965126e-24, 1.8081293645087937e-24, 1.8478684714210752e-24, 1.8876075783333563e-24, 1.9273466852456375e-24, 1.9670857921579186e-24, 2.0068248990701997e-24]

当我使用以下方法绘制这些数组时:

xlow = 0
xhigh = 2.0068248990701997e-24
figMassxHist = plt.figure()
axMassxHist = plt.axes()
axMassxHist.set_xlim([xlow, xhigh])
plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))

plt.bar(xbinmids, xbinned_ms, align='center')

生成的条形图为空。我想也许这些条太细了,看不到,所以尝试使用以下方法保存情节:

plt.savefig(gdir + "/m_x_pp.eps", format='eps', dpi = 72*5)

但结果仍然是空的。如何创建此数据的非空条形图?

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    我不确定是否可以根据您的数据绘制条形图,但可以使用其他图表,例如散点图(参见下面的代码)。希望没事:

    import matplotlib.pyplot as plt
    
    xbinned_ms =  [6.28e-16, 0, 0, 0, 0, 0, 6.28e-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.28e-16]
    
    xbinmids = [1.9869553456140592e-26, 5.960866036842178e-26, 9.934776728070296e-26, 1.3908687419298414e-25, 1.7882598110526532e-25, 2.185650880175465e-25, 2.583041949298277e-25, 2.9804330184210885e-25, 3.377824087543901e-25, 3.7752151566667126e-25, 4.172606225789524e-25, 4.569997294912336e-25, 4.967388364035148e-25, 5.36477943315796e-25, 5.762170502280771e-25, 6.159561571403584e-25, 6.556952640526395e-25, 6.954343709649207e-25, 7.351734778772019e-25, 7.749125847894831e-25, 8.146516917017643e-25, 8.543907986140454e-25, 8.941299055263266e-25, 9.338690124386079e-25, 9.73608119350889e-25, 1.0133472262631701e-24, 1.0530863331754513e-24, 1.0928254400877326e-24, 1.1325645470000137e-24, 1.1723036539122949e-24, 1.2120427608245762e-24, 1.2517818677368573e-24, 1.2915209746491384e-24, 1.3312600815614196e-24, 1.3709991884737009e-24, 1.410738295385982e-24, 1.4504774022982631e-24, 1.4902165092105445e-24, 1.5299556161228256e-24, 1.5696947230351067e-24, 1.6094338299473879e-24, 1.6491729368596692e-24, 1.6889120437719503e-24, 1.7286511506842314e-24, 1.7683902575965126e-24, 1.8081293645087937e-24, 1.8478684714210752e-24, 1.8876075783333563e-24, 1.9273466852456375e-24, 1.9670857921579186e-24, 2.0068248990701997e-24]
    
    plt.scatter(xbinmids, xbinned_ms)
    
    plt.show()
    
    plt.savefig(gdir + "/m_x_pp.eps", format='eps', dpi = 72*5)
    

    希望这会有所帮助! :)

    【讨论】:

    • 需要一个条形图来比较模拟结果和实验数据。不幸的是,其他图形类型,如散点图或茎,没有那么有用。我知道当像我一样输入 2 个数组时,Python 将为条形图创建默认宽度(0.8),但这对于我试图绘制的范围内的条形数量来说似乎太窄了。
    【解决方案2】:

    plt.bar 有一个width 参数,该参数可以是所有条形的单个宽度,也可以是每个单独条形的宽度数组。在这种情况下,可以使用前两个 x 值之间的差异,因为所有 bin 似乎都具有相同的宽度。 (如果您需要条形之间有一点间隙,可以将该数字乘以 0.8 左右。)

    plt.bar 在高度极小时似乎做错了什么。 ylims 搞砸了(将它们设置在 -1e12 和 1e12 之间),显示条形太小而无法看到。可以通过显式设置 y 限制来解决此错误:

    import matplotlib.pyplot as plt
    
    xbinned_ms =  [6.28e-16, 0, 0, 0, 0, 0, 6.28e-16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.28e-16]
    xbinmids = [1.9869553456140592e-26, 5.960866036842178e-26, 9.934776728070296e-26, 1.3908687419298414e-25, 1.7882598110526532e-25, 2.185650880175465e-25, 2.583041949298277e-25, 2.9804330184210885e-25, 3.377824087543901e-25, 3.7752151566667126e-25, 4.172606225789524e-25, 4.569997294912336e-25, 4.967388364035148e-25, 5.36477943315796e-25, 5.762170502280771e-25, 6.159561571403584e-25, 6.556952640526395e-25, 6.954343709649207e-25, 7.351734778772019e-25, 7.749125847894831e-25, 8.146516917017643e-25, 8.543907986140454e-25, 8.941299055263266e-25, 9.338690124386079e-25, 9.73608119350889e-25, 1.0133472262631701e-24, 1.0530863331754513e-24, 1.0928254400877326e-24, 1.1325645470000137e-24, 1.1723036539122949e-24, 1.2120427608245762e-24, 1.2517818677368573e-24, 1.2915209746491384e-24, 1.3312600815614196e-24, 1.3709991884737009e-24, 1.410738295385982e-24, 1.4504774022982631e-24, 1.4902165092105445e-24, 1.5299556161228256e-24, 1.5696947230351067e-24, 1.6094338299473879e-24, 1.6491729368596692e-24, 1.6889120437719503e-24, 1.7286511506842314e-24, 1.7683902575965126e-24, 1.8081293645087937e-24, 1.8478684714210752e-24, 1.8876075783333563e-24, 1.9273466852456375e-24, 1.9670857921579186e-24, 2.0068248990701997e-24]
    
    plt.bar(xbinmids, xbinned_ms, width=xbinmids[1]-xbinmids[0], ec='k', lw=1)
    plt.xlabel('bins')
    plt.ylabel('ms')
    plt.ylim(0, max(xbinned_ms)*1.1)
    plt.show()
    

    【讨论】:

    • 这个答案有帮助吗?