简体   繁体   English

如何使用 3 列创建散点图 plot

[英]How to create scatter plot with 3 columns

I have a df which looks:我有一个 df 看起来:

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:我想创建一个 plot (分散),其中:

  • x axis is column try_name x 轴是列try_name
  • y axis is column score y 轴是列score
  • The values (point in the graph) are according to column names (3 different colors)值(图中的点)根据列names (3 种不同颜色)

How can I create this plot?如何创建这个 plot?

Pure matplotlib solution:纯matplotlib解决方案:

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:使用 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() .您可以使用 seaborn 的swarmplot()stripplot()或 scatterplot( scatterplot()

For example with swarmplot() :例如swarmplot()

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

群图示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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