0
votes

I have a dataframe that looks like this:

x | y | value
10  20    0.1
20  5     0.2
30  15    0.7
40  10    0.5

I want to plot these values in a scatter plot and the color of each marker should be a color map calculated from the value column. The values go from 0 to 1.

Should I create a custom color map? How do I use it then?

EDIT:

Since some of the rows are not relevant to the scatter plot, I am plotting the relevant rows by iterating through the dataset:

for i,row in df.iterrows():
    ax.scatter(df.iloc[i]['y'], df.iloc[i]['x'], c= row['value'] , cmap='jet', edgecolors = 'r', linewidths = '1', marker='p', s=80)

Thank you! Kind Regards

1
Does this answer your question? Scatter plot and Color mapping in PythonPygirl
Something like plt.scatter(df['x'], df['y'], c=df['value'], cmap='inferno')?JohanC
I tried that @JohanC but then I got this error IndexError: tuple index out of rangeLuís Costa
Not what JohanC suggested, though. Why would use iterrows? Tbh, you should nearly never use iterrows.Mr. T
You could create a new dataframe with only the desired rows? Or you could create an array with the indices of the desired rows and use these as plt.scatter(df['x'][ind], df['y'][ind], c=df['value'][ind], cmap='inferno'). Also, it is strongly recommended not to use jet if you want to avoid strange highlights.JohanC

1 Answers

0
votes

If you really need to go through the dataframe row by row, you could add vmin=0 and vmax=1 to the call to plt.scatter to let it know your color range.

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame({'x': np.random.uniform(0, 30, 50),
                   'y': np.random.uniform(0, 15, 50),
                   'value': np.random.uniform(0, 1, 50)})
for _, row in df.iterrows():
    plt.scatter(row['x'], row['y'], c=row['value'],
                cmap='Reds', vmin=0, vmax=1,
                edgecolors='r', linewidths=1, marker='p', s=80)
plt.colorbar()
plt.tight_layout()
plt.show()

example plot