Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give a individual zorder value to every marker in a matplotlib scatter plot

I have a matplotlib scatter plot with many markers:

plt.scatter(x_position,y_position,c=z_position,s=90, cmap=cm.bwr,linewidth=1,edgecolor='k')

Sometimes the markers overlap. I want the zorder of each to be based on the z_position of the individual marker.

Is this possible in a scatterplot or would I have to have an separate line for each data point with its own zorder value?

Thank you.

like image 841
user1551817 Avatar asked Apr 30 '19 22:04

user1551817


1 Answers

import numpy as np
import matplotlib.pyplot as plt


x = np.array([0,1,0,1])
y = np.array([0,0,1,1])
z = np.array([8,4,6,2])

If you now call

plt.scatter(x, y, c=z, s=1000, marker="X", 
            cmap=plt.cm.bwr, linewidth=1, edgecolor='k')

markers overlap:

enter image description here

The last marker in the arrays is drawn last, hence the one with z=2 is in front.

You can sort the arrays by z to change the order of appearance.

order = np.argsort(z)
plt.scatter(x[order], y[order], c=z[order], s=1000, marker="X", 
            cmap=plt.cm.bwr, linewidth=1, edgecolor='k')

enter image description here

like image 104
ImportanceOfBeingErnest Avatar answered Nov 04 '22 05:11

ImportanceOfBeingErnest