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
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.