I would like that make scatter plot with different dots colors, dependent from column 'Value'
. For example all "rand"
dots would be blue and "qmax"
red. Here is my code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({
'State': [1000, 1002, 1001, 1003, 1000, 1003, 1001],
'Score': [62, 47, 55, 74, 31, 50, 60],
'Value': ['rand','qmax','rand','qmax','rand','qmax','qmax']
}, columns=['State', 'Score', 'Value'])
# Create figure with plot
fig, ax1 = plt.subplots()
ax1.scatter(df['State'], df['Score'])
plt.show()
My dataframe:
State Score Value
0 1000 62 rand
1 1002 47 qmax
2 1001 55 rand
3 1003 74 qmax
4 1000 31 rand
5 1003 50 qmax
6 1001 60 qmax
You could iterate the values in the 'Value'
column and, in each iteration, filter the dataframe and plot the filtered data:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({
'State': [1000, 1002, 1001, 1003, 1000, 1003, 1001],
'Score': [62, 47, 55, 74, 31, 50, 60],
'Value': ['rand','qmax','rand','qmax','rand','qmax','qmax']
}, columns=['State', 'Score', 'Value'])
print(df)
# Create figure with plot
fig, ax1 = plt.subplots()
for value in df['Value'].unique():
ax1.scatter(df[df['Value'] == value]['State'], df[df['Value'] == value]['Score'])
plt.show()
If you want to choose the color for each individual value in 'Value'
, you can define a dictionary as this one:
colors = {'rand': 'blue', 'qmax': 'red'}
And specify the color
parameter in the scatter
call:
for value in df['Value'].unique():
ax1.scatter(df[df['Value'] == value]['State'], df[df['Value'] == value]['Score'], color = colors[value])
You could also show a legend:
for value in df['Value'].unique():
ax1.scatter(df[df['Value'] == value]['State'], df[df['Value'] == value]['Score'], color = colors[value], label = value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.