1
votes

I am trying to make a simple scatter plot in python but I can't figure out how to scale (i) the size of the markers with the x-y axis range and (ii) the color of the markers with a 3rd variable (such that colors will span the full colormap range).

This is something trivial to do with IDL for instance but I can't find a simple solution with matplotlib.

Any idea would be appreciated !

Thanks

EDIT:

Here is a simple example.


import matplotlib.pyplot as plt

x = [10.,60.]
y = [30.,90.]
z = [3.,8.] # = diameters of x and y points 

plt.xlim(0., 100.)
plt.ylim(0., 100.)
plt.scatter(x,y,s=my_marker_size,marker='o')

How to define my_marker_size such that my points are represented by circles with diameters given by z on this plot ?

1
Did you not find it in the documentation? - Quang Hoang
well not really that's why I'm asking here. For the color-coding I ended up with something that seems to work but for the markersize I have no idea how to do it because the plt.scatter argument for size is the marker area "s" which cannot be scaled with the x-y axis values. At least I don't see it in the doc. Any advice welcome ;) - Nuanda
I’m not sure I understand what scaling with the x-y axis values means. You should add a sample data and expected output to your question. - Quang Hoang
OK no worries, I will edit my post with an example. - Nuanda
Any idea ?... :) - Nuanda

1 Answers

0
votes

plt.Circle creates circles that can be added to the plot. Setting the aspect ratio to 'equal' makes that the circles don't get stretched to look as ovals.

import matplotlib.pyplot as plt

x = [10., 60.]
y = [30., 90.]
z = [3., 8.] 

ax = plt.gca()
for xi, yi, diam in zip(x, y, z):
    ax.add_patch(plt.Circle((xi, yi), diam))
ax.set_aspect('equal')
plt.xlim(0., 100.)
plt.ylim(0., 100.)
plt.show()

example plot