Prateek Dewan
Prateek Dewan

Reputation: 1631

Matplotlib markers which plot and render fast

I'm using matplotlib to plot 5 sets of approx. 400,000 data points each. Although each set of points is plotted in a different color, I need different markers for people reading the graph on black and white print-outs. The issue I'm facing is that almost all of the possible markers available in the documentation at http://matplotlib.org/api/markers_api.html take too much time to plot and render while displaying. I could only find two markers which plot and render quickly, these are '-' and '--'. Here's my code:

plt.plot(series1,'--',label='Label 1',lw=5)
plt.plot(series2,'-',label='Label 2',lw=5)
plt.plot(series3,'^',label='Label 3',lw=5)
plt.plot(series4,'*',label='Label 4',lw=5)
plt.plot(series5,'_',label='Label 5',lw=5)

I tried multiple markers. Series 1 and series 2 plot quickly and render in no time. But series 3, 4, and 5 take forever to plot and AGES to display.

I'm not able to figure out the reason behind this. Does someone know of more markers that plot and render quickly?

Upvotes: 2

Views: 317

Answers (1)

hitzg
hitzg

Reputation: 12701

The first two ('--' and '-') are linestyles not markers. Thats why they are rendered faster.

It doesn't make sense to plot ~400,000 markers. You wont be able to see all of them... However, what you could do is to only plot a subset of the points. So add the line with all your data (even though you could probably also subsample that too) and then add a second "line" with only the markers. for that you need an "x" vectors, which you can subsample too:

# define the number of markers you want
nrmarkers = 100

# define a x-vector
x = np.arange(len(series3))
# calculate the subsampling step size
subsample = int(len(series3) / nrmarkers)
# plot the line
plt.plot(x, series3, color='g', label='Label 3', lw=5)
# plot the markers (using every `subsample`-th data point)
plt.plot(x[::subsample], series3[::subsample], color='g', 
        lw=5, linestyle='', marker='*')

# similar procedure for series4 and series5

Note: The code is written from scratch and not tested

Upvotes: 2

Related Questions