【问题标题】:plotting histogram as line graph in matplot using x and y values in python在 python 中使用 x 和 y 值在 matplot 中将直方图绘制为折线图
【发布时间】:2013-02-11 19:21:55
【问题描述】:

我有两个 X 轴和 Y 轴的输入值。 我想将直方图绘制为折线图。附图中提供了一个示例。 我无法使用这些值绘制直方图。

ys = [0.21428571428571427, 0.14285714285714285, 0.047619047619047616, 0.11904761904761904, 0.09523809523809523, 0.09523809523809523, 0.023809523809523808, 0.09523809523809523, 0.11904761904761904, 0.047619047619047616]

xs = [0,1,2,3,4,5,6,7,8,9]

我试过了

plt.hist(xs,ys)
plt.show()

但它失败了。

我对如下图感兴趣,最好在直方图上有和没有线。

X 轴和 Y 轴分配有 xs 和 ys 的值。

【问题讨论】:

    标签: python matplotlib histogram linegraph


    【解决方案1】:

    你需要的函数是bar。 这是一个最小的例子:

    >>> bar(xs, ys, width=1, align='center', color='brown')
    >>> plot(xs, ys, color='purple', lw=2, marker='s')
    

    【讨论】:

    • 我试过这个,但是没有用。我怎样才能创建折线图。(根本没有直方图)
    • @user1988876 对plot 的调用执行此操作。你是什​​么意思“没有工作”?调用需要与您所做的导入相对应。根据您问题中的代码,应该是plt.barplt.plot。不要忘记plt.figure()plt.show() 如果您以非交互方式执行此操作,但这是我假设您知道的基本内容。
    • 请注意直方图与条形图不同!
    • @Dan Sure。但如果你有x y,你可能想要后者。
    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2021-10-07
    • 2020-06-19
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多