【问题标题】:How to plot a distribution that has already been computed?如何绘制已经计算好的分布?
【发布时间】:2021-07-13 23:11:01
【问题描述】:

我有如下数据,其中第一列是 x 坐标间隔,y 是值。

0   1
4   2
6   1
10  0

例如,[0, 4) 的值为 1,[4, 6) 的值为 2,[6, 10) 的值为 1。超过 10 为 0,因此无需绘制任何内容。

ggplot2 可以用没有像这样压缩的数据绘制直方图。但我不知道它是否适合这样的压缩数据。

谁能告诉我在 R 和 python 中绘制这样的数据的最佳方法?

编辑:我不确定 gnuplot 是否适合这个。但也需要以文本格式绘制这种分布数据。我看到 gnuplot 可以以文本格式绘制图形,如果适合用于此目的,也欢迎使用 gnuplot 中的解决方案。

【问题讨论】:

  • 您的问题还没有解决吗?您没有投票或接受任何答案有什么特别的原因吗?

标签: r matplotlib ggplot2 seaborn gnuplot


【解决方案1】:

您可以绘制条形图,使用 x 值进行定位、y 值表示高度以及 x 值的差值表示宽度。 align='edge' 让条形图从给定的 x 位置开始(默认会以这些位置为中心)。

from matplotlib import pyplot as plt
import numpy as np

x = [0, 4, 6, 10]
y = [1, 2, 1, 0]
plt.bar(x[:-1], y[:len(x)-1], width=np.diff(x), color='crimson', ec='white', align='edge')
plt.xticks(x)
plt.show()

【讨论】:

    【解决方案2】:

    Gnuplot 的绘图风格 steps 可以满足您的需求。

    $DATA << EOD
    0   1
    4   2
    6   1
    10  0
    EOD
    
    set tics nomirror
    set border 3
    set yrange [0:*]
    
    plot $DATA with steps lw 2 title "plotstyle 'steps'"
    

    Ascii 输出(实际上是 UTF-8,但对此无关紧要)

    gnuplot> set term dumb size 90,20
    
    Terminal type is now 'dumb'
    Options are 'feed  size 90, 20 aspect 2, 1 mono'
    gnuplot> plot $DATA with steps notitle
    
                                                                                              
      2 ++                               *****************                                    
        |                                *               *                                    
        |                                *               *                                    
        |                                *               *                                    
        |                                *               *                                    
        |                                *               *                                    
        |                                *               *                                    
        |                                *               *                                    
      1 **********************************               **********************************   
        |                                                                                 *   
        |                                                                                 *   
        |                                                                                 *   
        |                                                                                 *   
        |                                                                                 *   
        |                                                                                 *   
        |                                                                                 *   
      0 +---------------------------------------------------------------------------------*   
        0               2                4               6                8               10  
    

    【讨论】:

    • 如何将其绘制成 ascii 格式?如何让 gnuplot 绘制 TSV 文件中的数据?
    【解决方案3】:

    ggplot2

    使用geom_step():

    df <- data.frame("x" = c(0, 4, 6, 10), "y" = c(1, 2, 1, 0))
    ggplot(df) + geom_step(aes(x=x, y=y))
    

    matplotlib

    plt.step() 包装器与where="post" 一起使用:

    x = [0, 4, 6, 10]
    y = [1, 2, 1, 0]
    plt.step(x, y, where="post")
    

    或者plt.plot()drawstyle="steps-post"

    plt.plot(x, y, drawstyle="steps-post")
    

    或者DataFrame.plot()drawstyle="steps-post"

    df = pd.DataFrame({"x": x, "y": y})
    df.plot("x", "y", drawstyle="steps-post")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2019-11-22
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多