【问题标题】:Histogram with two variable in matplotlibmatplotlib中有两个变量的直方图
【发布时间】:2020-09-14 07:20:58
【问题描述】:

我有一个这样的数据集:

ID  z  N
0   0.15    69.0
1   0.25    208.0
2   0.35    402.0
3   0.45    223.0
4   0.55    327.0
5   0.65    136.0
6   0.75    136.0
7   0.85    136.0
8   0.95    136.0
9   1.05    136.0
10  1.15    136.0
11  1.25    136.0
12  1.35    136.0
13  1.45    136.0
14  1.55    136.0
15  1.65    136.0

我想制作一个情节

我找不到出路。一个简单的plt.hist() 是一个单函数图。或者 plt.bar(z,N) 不会消除条之间的线条。

【问题讨论】:

    标签: python matplotlib plot histogram


    【解决方案1】:

    这是因为plt.hist 需要一个值列表,它会根据这些值计算频率。由于您已经有了频率,您可以重新制作值列表并让plt.hist 以它的方式工作

    import matplotlib.pyplot as plt
    
    z = [0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95, 1.05, 1.15, 1.25, 1.35, 1.45, 1.55, 1.65]
    N = [69.0, 208.0, 402.0, 223.0, 327.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0, 136.0]
    hist_vals = []
    for n,zz in zip(N,z):
        hist_vals += [zz]*int(n)
    plt.hist(hist_vals,bins=z+[1.7], histtype='step', edgecolor='k')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-16
      • 2018-04-18
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多