I'd like to plot a stacked histogram plot, but I have data columns of uneven size, and indeed data type:
bovine_eta = bovine['Emissivity'].dropna()
equine_eta = equine['Emissivity'].dropna()
ovine_eta = ovine['Emissivity'].dropna()
bovine_eta.sort()
equine_eta.sort()
ovine_eta.sort()
print(bovine_eta)
['0.93' '0.93' '0.93' '0.95' '0.95' '0.95' '0.95' '0.95' '0.95' '0.95'
'0.95' '0.95' '0.95' '0.95' '0.95' '0.95' '0.96' '0.96' '0.97' '0.97'
'0.97' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98'
'0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98'
'0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98'
'0.98' '0.985' '0.985' '0.985' '1']
print(equine_eta)
['0.95' '0.95' '0.95' '0.96' '0.97' '0.97' '0.97' '0.98' '0.98' '0.98', '0.98' '0.99' '0.99']
print(ovine_eta)
['0.95' '0.97' '0.97-0.98' '0.98' '0.98' '0.98' '0.98' '0.98' '0.98', '0.98' '0.98']
I then try
plt.hist([bovine_eta, equine_eta, ovine_eta], stacked=True)
and get the resulting plot:
This seems very weird, but obviously not far off from what I'd like actually want. How do you fix the x-axis?
The main problem is that all values are strings. For a histogram it helps to convert them to floats. A next problem is that the third entry of ovine
is a string that can't be converted to a float. '0.97-0.98'
could be replaced by an intermediate value.
As the data is almost discrete, it helps to explicitly provide bin edges aligned with these values.
import matplotlib.pyplot as plt
import numpy as np
bovine_eta = ['0.93','0.93','0.93','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.95','0.96','0.96','0.97','0.97','0.97','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.985','0.985','0.985','1']
equine_eta = ['0.95','0.95','0.95','0.96','0.97','0.97','0.97','0.98','0.98','0.98', '0.98','0.99','0.99']
ovine_eta = ['0.95','0.97','0.97-0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98','0.98']
ovine_eta[2] = '0.975' # '0.97-0.98' isn't a valid number
bovine_eta = np.array(bovine_eta, dtype=float)
equine_eta = np.array(equine_eta, dtype=float)
ovine_eta = np.array(ovine_eta, dtype=float)
plt.hist([bovine_eta, equine_eta, ovine_eta],
bins=np.arange(0.93, 1.01, 0.01),
stacked=True, label=['bovine', 'equine', 'ovine'])
plt.legend()
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.