用matplotlib同時繪制兩個直方圖

[英]Plot two histograms at the same time with matplotlib


I created a histogram plot using data from a file and no problem. Now I wanted to superpose data from another file in the same histogram, so I do something like

我使用文件中的數據創建了一個直方圖,沒有問題。現在我想將來自另一個文件的數據疊加到相同的直方圖中,所以我做了一些類似的事情

n,bins,patchs = ax.hist(mydata1,100)
n,bins,patchs = ax.hist(mydata2,100)

but the problem is that for each interval, only the bar with the highest value appears, and the other is hidden. I wonder how could I plot both histograms at the same time with different colors.

但問題是,對於每一個區間,只有具有最高值的bar出現,而另一個則隱藏。我想知道如何同時用不同的顏色繪制這兩個直方圖。

6 个解决方案

#1


254  

Here you have a working example:

這里有一個工作示例:

import random
import numpy
from matplotlib import pyplot

x = [random.gauss(3,1) for _ in range(400)]
y = [random.gauss(4,2) for _ in range(400)]

bins = numpy.linspace(-10, 10, 100)

pyplot.hist(x, bins, alpha=0.5, label='x')
pyplot.hist(y, bins, alpha=0.5, label='y')
pyplot.legend(loc='upper right')
pyplot.show()

enter image description here

#2


69  

The accepted answers gives the code for a histogram with overlapping bars, but in case you want each bar to be side-by-side (as I did), try the variation below:

被接受的答案給出了帶有重疊條的直方圖的代碼,但是如果你想讓每個條都是並排的(就像我一樣),試試下面的變化:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-deep')

x = np.random.normal(1, 2, 5000)
y = np.random.normal(-1, 3, 2000)
bins = np.linspace(-10, 10, 30)

plt.hist([x, y], bins, label=['x', 'y'])
plt.legend(loc='upper right')
plt.show()

enter image description here

Reference: http://matplotlib.org/examples/statistics/histogram_demo_multihist.html

參考:http://matplotlib.org/examples/statistics/histogram_demo_multihist.html

EDIT [2018/03/16]: Updated to allow plotting of arrays of different sizes, as suggested by @stochastic_zeitgeist

編輯[2018/03/16]:根據@stochastic_zeitgeist的建議,更新允許繪制不同大小的數組。

#3


9  

In the case you have different sample sizes, it may be difficult to compare the distributions with a single y-axis. For example:

在不同樣本容量的情況下,很難將分布與單個y軸進行比較。例如:

import numpy as np
import matplotlib.pyplot as plt

#makes the data
y1 = np.random.normal(-2, 2, 1000)
y2 = np.random.normal(2, 2, 5000)
colors = ['b','g']

#plots the histogram
fig, ax1 = plt.subplots()
ax1.hist([y1,y2],color=colors)
ax1.set_xlim(-10,10)
ax1.set_ylabel("Count")
plt.tight_layout()
plt.show()

hist_single_ax

In this case, you can plot your two data sets on different axes. To do so, you can get your histogram data using matplotlib, clear the axis, and then re-plot it on two separate axes (shifting the bin edges so that they don't overlap):

在這種情況下,您可以在不同的軸上繪制兩個數據集。為此,您可以使用matplotlib獲得直方圖數據,清除軸,然后在兩個單獨的軸上重新繪制它(移動bin邊,使它們不重疊):

#sets up the axis and gets histogram data
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.hist([y1, y2], color=colors)
n, bins, patches = ax1.hist([y1,y2])
ax1.cla() #clear the axis

#plots the histogram data
width = (bins[1] - bins[0]) * 0.4
bins_shifted = bins + width
ax1.bar(bins[:-1], n[0], width, align='edge', color=colors[0])
ax2.bar(bins_shifted[:-1], n[1], width, align='edge', color=colors[1])

#finishes the plot
ax1.set_ylabel("Count", color=colors[0])
ax2.set_ylabel("Count", color=colors[1])
ax1.tick_params('y', colors=colors[0])
ax2.tick_params('y', colors=colors[1])
plt.tight_layout()
plt.show()

hist_twin_ax

#4


7  

Here is a simple method to plot two histograms, with their bars side-by-side, on the same plot when the data has different sizes:

這里有一個簡單的方法來繪制兩個直方圖,當數據有不同的大小時,這些直方圖會並排在同一塊圖上:

def plotHistogram(p, o):
    """
    p and o are iterables with the values you want to 
    plot the histogram of
    """
    plt.hist([p, o], color=['g','r'], alpha=0.8, bins=50)
    plt.show()

#5


3  

It sounds like you might want just a bar graph:

聽起來你可能只想要一個條形圖:

Alternatively, you can use subplots.

或者,您可以使用子情節。

#6


0  

Just in case you have pandas (import pandas as pd) or are ok with using it:

如果你有熊貓(以熊貓的形式進口)或者可以使用:

test = pd.DataFrame([[random.gauss(3,1) for _ in range(400)], 
                     [random.gauss(4,2) for _ in range(400)]])
plt.hist(test.values.T)
plt.show()

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/07/29/725789be13ffe148590874c8b7d84f92.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com