简体   繁体   中英

Is there a way to plot multiple cumulative histograms on the same axes, where the datasets are normalized

I am attempting to plot two datasets on the same axes using matplotlib.plt.hist(). I would like the datasets to appear on the same y axis such that the 100% value of each dataset will appear on the same spot on the plot. These datasets have different amounts of data in them. I am generating cumulative distribution plots, both for total area and for percent density. I have tried to use density = True as an argument, however the plots were not the same shape as when plotted separately. Here is the code I have used thus far!


data = pd.Series(condensed['slope'])
data_mtl = pd.Series(montreal['slope'])

fig, ax = plt.subplots(figsize = (60, 20))
ax.hist([data, data_mtl], bins = 200, color=['g','r'], cumulative = True, histtype = 'step')
ax.set_xlabel('ΔCO/ΔCO₂ (ppb/ppm)')
ax.set_ylabel("% Density")

for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(50)
    
ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=len(data_mtl)))
plt.show()

绘图生成

EDIT: Separate plots with 80th percentile line vs with density = True

9月1日

9月2日

with density = True:

一起

Provided you use density=True , cumulative=True for all plots, it should be just fine:

import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.normal(size=100)
data2 = np.random.rand(4000)    
plt.hist(data1, cumulative=True, density=True)
plt.hist(data2, cumulative=True, density=True)
plt.show()

在此处输入图片说明

If your x-axis is not large enough to include all the data and has a sparse region, it could appear that it "does not reach 1.0 ". I would try again with density=True . Also, cumulative=True is required to guarantee that the max bin height will be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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