How to plot a distribution that has already been computed?

Viewed 125

I have data like the following, where the 1st column is the x coord interval and y is the value.

0   1
4   2
6   1
10  0

For example, [0, 4) has the value of 1, [4, 6) has the value of 2, [6, 10) has the value of 1. Beyond 10 is zero, so nothing needs to be plotted.

ggplot2 can plot histogram with data that has not been compressed like this. But I don't know whether it is the right tool for a compressed data like this.

Could anybody let me know the best way to plot data like this in R and python?

EDIT: I am not sure gnuplot is the appropriate for this or not. But plot this kind of distribution data in text format is also needed. I see that gnuplot can plot figures in text format, if it is appropriate to use for this purpose, a solution in gnuplot is also welcome.

2 Answers

You can draw a bar plot, using the x-values for positioning, the y-values for the heights and the differences of the x-values for the widths. align='edge' lets the bars start at the given x positions (the default would be centered to these positions).

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()

histogram given bin boundaries and values

Gnuplot has a plot style steps that may do what you want.

$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'"

enter image description here

Ascii output (actually UTF-8 but for this purpose it doesn't matter)

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  
Related