[英]Plotting multiple histograms on the same plot with different x-axis values
我试图从包含浮点值的数据库中获取多年数据(例如,从2005-2007年)。
2005年值(已排序): [0.512, 0.768, 1, 1.5..., 100]
2006值(排序): [0.288, 0.512..., 300, 350]
,依此类推。
我想生成一个CDF图(使用ax.hist()函数),该图使我能够将每年的图绘制到一张图中。 我当前的代码如下所示:
num_bins = 100
fig, ax = plt.subplots(figsize=(8, 4))
years = ['2005', '2006', '2007']
for year in years:
df = pd.read_sql_query(query, conn) #sorted
n, bins, patches = ax.hist(df.values, num_bins, normed = 1, histtype='step', cumulative=True, label=str(year))
ax.grid(True)
ax.legend(loc='right')
ax.set_xlabel('Values')
ax.set_ylabel('CDF plot')
plt.show()
但是,这给了我一个带有多个CDF直方图但x轴变化(未排序)的图。 我的x轴值为: 0.512, 0.768, 1, 1.5..., 100, 0.288, ..., 300, 350
。 它将第二年新发现的值附加到第一年的x轴值,而不是使用相同的比例尺重新绘制。
如何确保以共同且动态变化的比例(最终结果)生成所有CDF图,例如: 0.288, 0.512, 0.768, 1, 1.5..., 100..., 300, 350
。
我发现了问题:
df = pd.read_sql_query(query, conn) #sorted
这给我返回了一个带有df.values
的数据帧,该数据帧是一个排序字符串而不是数字的数组。 将其格式化为数字后,图将正确生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.