2

I have a dataframe:

d = {'x':[38.23750, 34.07029, 49.71443, 37.77493, 40.71427], 
     'y':[-117.39588, -104.35108, 7.30776,-122.41942, -74.00597],
    'size':[300, 20, 100, 150, 80],
    'density':[10,20,30,40,50]}
df = pd.DataFrame(data=d)

    x               y      size   density
0   38.23750    -117.39588  300     10
1   34.07029    -104.35108  20      20
2   49.71443    7.30776     100     30
3   37.77493    -122.41942  150     40
4   40.71427    -74.00597   80      50

I want to plot the dots so that the size is determined using the df[size] column. I also want to add a color gradient from light blue to dark blue depending on the df[density] column (the smallest value is light blue, the last value is dark blue).

I'm plotting like this, but i dont know how to add a gradient:

x = df['x'] 
y = df['y'] 
s = df['size']
c  =  df['density']
plt.scatter(x, y, s=s, c=c, alpha=0.5)

enter image description here

But I would like such colors (painted in paint)

enter image description here

1 Answer 1

3

You need to specify a colormap:

plt.scatter('x', 'y', s='size', c='density', data=df, alpha=0.5, cmap='Blues')

enter image description here

PS: it's easier to use the data keyword to specify columns as shown in the answer than assigning the dataframe columns to variables.


If you don't find a suitable colormap, you can make your own, e.g. from white to blue:

from matplotlib.colors import LinearSegmentedColormap
blue_cm = LinearSegmentedColormap.from_list('Blue', ['w', 'b'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.