it is possible to get a histogram style in ListPlot
using InterpolationOrder->0
testdata = {{1, 3}, {3, 4}, {4, 3}, {5, 8}, {7, 6}, {9, 4}};
ListPlot[testdata, Joined -> True, InterpolationOrder -> 0,
Epilog -> {PointSize[Large], Point[testdata]}]
Note that the data points are sitting at the corner of the histograms at the left end of the horizontal lines. My question is:
How could I plot the same data set with the data points being centered on the horizontal line segments? For equidistant data points this is of course a simple shift. But how to do this for irregularly gridded data?
Edit 1:
To clarify: Of course this is possibly not very meaningfull in irregularly spaced cases, and centered is the wrong term, however, in the example case the data point connection I was looking for could look like the following:
Answer
This uses Nearest
to build a NearestFunction
(nf
) for your testdata
. This is like InterpolationOrder -> 0
except that it is centered because it does as the name implies: gives the nearest value.
testdata = {{1, 3}, {3, 4}, {4, 3}, {5, 8}, {7, 6}, {9, 4}};
nf = Nearest[Rule @@@ testdata];
{min, max} = {Min@# - 1, Max@# + 1} &@testdata[[All, 1]];
Plot[ nf[x], {x, min, max},
AxesOrigin -> {0, 0},
Epilog -> {PointSize[Large], Point[testdata]}
]
Comments
Post a Comment