简体   繁体   中英

How to create scatter plot with 3 columns

I have a df which looks:

df_results = pd.DataFrame(data={})
df_results['names'] = ['James', 'Lucas', 'Henry', 'James', 'Lucas', 'Henry']
df_results['try_name'] = ["try_1", "try_1", "try_1", "try_2", "try_2", "try_2"]
df_results['score'] = [0.7, 0.9, 0.3, 0.91, 0.1, 0.2]

   names try_name  score
0  James    try_1   0.70
1  Lucas    try_1   0.90
2  Henry    try_1   0.30
3  James    try_2   0.91
4  Lucas    try_2   0.10

I want to create a plot (scatter) where:

  • x axis is column try_name
  • y axis is column score
  • The values (point in the graph) are according to column names (3 different colors)

How can I create this plot?

Pure matplotlib solution:

fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(111)
for name in df_results.names.unique():
    ax.scatter(df_results[df_results.names == name].try_name, 
               df_results[df_results.names == name].score, label=name)
ax.legend()

Using pandas plot:

fig = plt.figure(figsize=(7,4))
ax = fig.add_subplot(111)
for i,name in enumerate(df_results.names.unique()):
    df_results[df_results.names == name].plot.scatter('try_name', 'score', 
                                                      ax=ax, color='C{}'.format(i), 
                                                      label=name)
ax.legend()

You could use seaborn's swarmplot() , stripplot() , or scatterplot() .

For example with swarmplot() :

import seaborn as sns
sns.swarmplot(data=df_results, x='try_name', y='score', hue='names')

群图示例

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM