数据不均匀的 Matplotlib 堆积图
Matplotlib stacked plot with uneven data
我想绘制堆叠直方图,但我有大小不均匀的数据列,而且确实是数据类型:
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']
我再试试
plt.hist([bovine_eta, equine_eta, ovine_eta], stacked=True)
并得到结果图:
这看起来很奇怪,但显然离我真正想要的不远。你如何固定 x 轴?
主要问题是所有值都是字符串。对于直方图,它有助于将它们转换为浮点数。下一个问题是 ovine
的第三个条目是一个无法转换为浮点数的字符串。 '0.97-0.98'
可以替换为中间值。
由于数据几乎是离散的,因此有助于明确提供与这些值对齐的 bin 边缘。
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()
我想绘制堆叠直方图,但我有大小不均匀的数据列,而且确实是数据类型:
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']
我再试试
plt.hist([bovine_eta, equine_eta, ovine_eta], stacked=True)
并得到结果图:
这看起来很奇怪,但显然离我真正想要的不远。你如何固定 x 轴?
主要问题是所有值都是字符串。对于直方图,它有助于将它们转换为浮点数。下一个问题是 ovine
的第三个条目是一个无法转换为浮点数的字符串。 '0.97-0.98'
可以替换为中间值。
由于数据几乎是离散的,因此有助于明确提供与这些值对齐的 bin 边缘。
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()