[英]Plotting multiple histograms on the same plot with different x-axis values
I am trying to fetch multiple years of data (eg, from 2005-2007) from a database that contains values in float format. 我试图从包含浮点值的数据库中获取多年数据(例如,从2005-2007年)。
2005 values (sorted): [0.512, 0.768, 1, 1.5..., 100]
2005年值(已排序):
[0.512, 0.768, 1, 1.5..., 100]
2006 values (sorted): [0.288, 0.512..., 300, 350]
and so on. 2006值(排序):
[0.288, 0.512..., 300, 350]
,依此类推。
I want to generate a CDF plot (using ax.hist() function) that enables me to plot each year into a single graph. 我想生成一个CDF图(使用ax.hist()函数),该图使我能够将每年的图绘制到一张图中。 My current code looks like this:
我当前的代码如下所示:
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()
However, this gives me a single plot with multiple CDF histograms but varying x-axis (unsorted). 但是,这给了我一个带有多个CDF直方图但x轴变化(未排序)的图。 My x-axis values are:
0.512, 0.768, 1, 1.5..., 100, 0.288, ..., 300, 350
. 我的x轴值为:
0.512, 0.768, 1, 1.5..., 100, 0.288, ..., 300, 350
。 It appends the newly found values in the second year to the first year x-axis values instead of re-plotting using the same scale. 它将第二年新发现的值附加到第一年的x轴值,而不是使用相同的比例尺重新绘制。
How can I ensure that all CDF plots get generated for a common and dynamically varying scale (end-result) such as: 0.288, 0.512, 0.768, 1, 1.5..., 100..., 300, 350
. 如何确保以共同且动态变化的比例(最终结果)生成所有CDF图,例如:
0.288, 0.512, 0.768, 1, 1.5..., 100..., 300, 350
。
I found the problem: 我发现了问题:
df = pd.read_sql_query(query, conn) #sorted
This was returning me a data frame with df.values
as an array of sorted strings and not numbers. 这给我返回了一个带有
df.values
的数据帧,该数据帧是一个排序字符串而不是数字的数组。 After formatting this to numbers, the plots generated correctly. 将其格式化为数字后,图将正确生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.