Plot with fewer markers than data points (or a better way to plot CDFs?) [matplotlib, or general plotting help]
Asked Answered
M

2

46

I am plotting Cumulative Distribution Functions, with a large number of data points. I am plotting a few lines on the same plot, which are identified with markers as it will be printed in black and white. What I would like are markers evenly spaced in the x-dimension. What I am getting is one marker per data point (and given the number of points, they all overlap)

I'm not sure if it's my understanding of how to plot well, or just a lack of understanding matplotlib. I can't find a 'marker frequency' setting.

An easy solution for one line would be to take every N'th value from the line, and use that as a separate line with linestyle='', but I would like the markers to be vertically aligned, and the different x arrays have different lengths.

# in reality, many thousands of values
x_example = [ 567, 460, 66, 1034, 275, 26, 628, 99, 287, 157, 705, 421, 1093, \ 
     139, 204, 14, 240, 179, 94, 139, 645, 670, 47, 520, 891, 450, 56, 964,   \
     1728, 99, 277, 356, 1628, 745, 364, 88, 112, 810, 816, 523, 401, 89,     \ 
     278, 917, 370, 53, 39, 90, 853, 356 ] 
x = sort(x_example)
y = linspace(0,1,len(x))

ax = subplot(1,1,1)
plots[w] = ax.plot(x,y, marker='o')
Maladjusted answered 11/1, 2010 at 6:34 Comment(1)
Related solution for arclength marker spacing: #17407258Divalent
P
71

You can do plot(x,y,marker='o',markevery=5) to mark every fifth point, but I don't think there is any built-in support for setting marks at even intervals. You could decide on the x locations where you want the marks, use e.g. numpy.searchsorted to find which data points the locations fall between, and then interpolate between the neighboring points to find the y coordinates.

Paramour answered 11/1, 2010 at 17:49 Comment(2)
Well, that's certainly nicer than my approach would have been. Time to look up searchsorted!Maladjusted
Alternatively, markevery supports a float input which markers will be spaced at approximately equal distances along the line; the distance along the line between markers is determined by multiplying the display-coordinate distance of the axes bounding-box diagonal by the value matplotlib.org/api/…Drusilla
K
2

The "markevery" can take different input type, As it is said in the documentaion: A float specifies the distance between markers as a fraction of the Axes diagonal in screen space. So for example markevery=0.1 will make 10 equally distanced marker along x-axis or markerevery=(0.1,0.1) will make a diagnolly equall distanced markers.

Kristofor answered 19/10, 2022 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.