Matplotlib:如何使用 pandas 图 api 在散点图中绘制空圆?

Matplotlib: How to plot an empty circle in an scatter plot using pandas plot api?

我正在尝试使用 pandas api 绘制散点图,其中每个点都是一个空圆,仅具有边框颜色和透明度。我在这段代码中尝试了很多调整:

 ax = ddf.plot.scatter(
        x='espvida', 
        y='e_anosestudo', 
        c=ddf['cor'],
        alpha=.2,
        marker='o');

生成的图如下所示:

如果你仔细观察这些点:

您会看到它们具有透明的填充颜色和边框。我希望它只有一个透明边框。我会怎么做?

来自matplotlib scatter doc

edgecolors : color or sequence of color, optional, default: 'face'. The edge color of the marker. Possible values:

  • 'face': The edge color will always be the same as the face color.

  • 'none': No patch boundary will be drawn.

  • A matplotib color.

For non-filled markers, the edgecolors kwarg is ignored and forced to 'face' internally

尝试添加:edgecolors='none':

 ax = ddf.plot.scatter(
        x='espvida', 
        y='e_anosestudo', 
        c=ddf['cor'],
        alpha=.2,
        marker='o',
        edgecolors='none);

我似乎无法让它与 DataFrame.plot.scatter 一起工作;它似乎不尊重 facecolors='none' kwarg,可能是因为某些默认颜色参数被传递给 plt.scatter

相反,回到 matplotlib,指定 facecolors='none' 并将 edgecolors 设置为 df 中代表颜色的列。

示例数据

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

df = pd.DataFrame({'x': np.random.normal(1,1,1000),
                   'y': np.random.normal(1,1,1000),
                   'color': list('rgby')*250})

plt.scatter(df.x.values, df.y.values, facecolors='none', edgecolors=df['color'], alpha=0.2, s=100)
plt.show()