如何在散点图中按所需顺序对 y 轴进行排序?

How to sort y axis in desired order in scatter plot?

我想用 x 轴主题和 y 轴内容为以下 DataFrame 制作散点图。

In[18]: test=pd.read_excel('test.xlsx')
In[19]: test
Out[19]:    topic content
        0    A1       a
        1    A1       b
        2    A2       b
        3    A2       c
        4    A2       e
        5    A3       a
        6    A3       c
        7    A3       d
        8    A4       b
        9    A4       c

下面是我现在的剧情:

如何以不同的顺序对 y 轴进行排序?比如['b', 'c', 'a', 'd', 'e']下面有'b'?

如果 x 轴的顺序不重要,您可以使用 pandas Categorialsort_values():

df = pd.DataFrame([['A1','a'], ['A1','b'], ['A2','b'], ['A2','c'], ['A2','e'], ['A3','a'], ['A3','c'], ['A3','d'], ['A4','b'], ['A4','c']], columns=['topic','content'])

order = ['b', 'c', 'a', 'd', 'e']
df['content'] = pd.Categorical(df['content'], order)
df.sort_values(by=['content'], inplace=True)

plt.scatter(df['topic'], df['content'])

编辑 另一种解决方案是将 content 的每个值替换为整数 df['content'] = [order.index(x) for x in df['content']] 并设置 yticks:

order = ['b', 'c', 'a', 'd', 'e']
df = pd.DataFrame([['A1','a'], ['A1','b'], ['A2','b'], ['A2','c'], ['A2','e'], ['A3','a'], ['A3','c'], ['A3','d'], ['A4','b'], ['A4','c']], columns=['topic','content'])

df['content'] = [order.index(x) for x in df['content']]

plt.yticks(range(len(order)), order)
plt.scatter(df['topic'], df['content'])