【问题标题】:When I plot 2 columns one over the other, a different graph is plotted当我将 2 列一个接一个地绘制时,会绘制一个不同的图表
【发布时间】:2019-03-27 12:25:56
【问题描述】:

我有来自 2 个相同大小的 np.array 的 2 列。当我绘制唯一的一个时,我得到了这个结果:

plt.figure(figsize=(70,10))

for i,h in enumerate(clean_head):
    
    plt.subplot(1,6,i+1)
    #plt.hist(non_fire[:,i],alpha=.3)
    plt.hist(fire[:,i],alpha=.3)
    plt.title(clean_head[i])
    
   # plt.tight_layout()

当我绘制两者时,我得到了这个:

plt.figure(figsize=(70,10))

for i,h in enumerate(clean_head):
    
    plt.subplot(1,6,i+1)
    plt.hist(non_fire[:,i],alpha=.3)
    plt.hist(fire[:,i],alpha=.3)
    plt.title(clean_head[i])
    
   # plt.tight_layout()

两个直方图中没有一个与初始值相同。我不明白哪个是粉红色的,哪个是浅蓝色的。

我还有 16 个这样的地块,而且我对所有地块都有同样的问题。

【问题讨论】:

    标签: python plot histogram


    【解决方案1】:

    在第二组图中,蓝色直方图用于“非火灾”情况,橙色直方图用于“火灾”情况。如果您要绘制第三个直方图,它将是绿色的。一般来说,您可以使用参数color 更改给定直方图的颜色。

    您的直方图发生变化的原因是您的数组具有不同的值范围。您可以通过显式赋予 bin 函数来解决此问题:

    import matplotlib.pyplot as plt
    import numpy as np
    
    a = np.random.rand(100)
    b = np.random.rand(100)*2
    
    bins = np.linspace(min(np.min(a), np.min(b)), max(np.max(a), np.max(b)), 10)
    
    plt.figure(figsize=(7,5))
    plt.hist(a,alpha=.3, bins=bins)
    #plt.hist(b,alpha=.3, bins=bins) #toggle this to see the effect
    

    另请注意,直方图函数返回它使用的 bin 列表。

    希望对您有所帮助。

    【讨论】:

    • 这真的很有帮助,非常感谢。试图为我写的案例实现 bin 分配: bins = np.linspace(min(np.min(non_fire[:,i]), np.min(fire[:,i])), max(np.max (non_fire[:,i]), np.max(fire[:,i])), 10) 但是我得到这个错误:TypeError: cannot perform reduce with flexible type
    • 你的火对象和非火对象是什么结构(例如 numpy 数组)以及值的数据类型是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多