When I plot 2 columns one over the other, a different graph is plotted

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 08:30:25

问题


I have 2 columns from 2 np.arrays of the same size. When I plot the only the one I get this result:

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()

When I plot both I get this:

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()

Where no one of the two histograms is the same with the initial. I dont understand which is the pink and which is the lightblue graph.

I have 16 more plots like that and I have the same problem with all of them.


回答1:


in the second set of plots the blue histogram is for the "non-fire"-case and the orange one is for the "fire"-case. If you would plot a third histogram, it would be green. In general you can change the color of a given histogram using the parameter color.

The reason why your histograms change is that your arrays have different value ranges. You can fix this by explicitly giving bins the the function:

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

Also note that histogram function returns the list of bins that it uses.

Hope that helps.



来源:https://stackoverflow.com/questions/55377172/when-i-plot-2-columns-one-over-the-other-a-different-graph-is-plotted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!