This question already has answers here:
matplotlib scatterplot x axis labels

(2个答案)


2年前关闭。




当我向 Pandas 图添加c时,x刻度标签消失了。有人知道如何将它们重新添加吗?
import pandas as pd

df = pd.DataFrame(
    {'mean': {0: 10,
              1: 16,
              2: 18,
              3: 22,
              4: 30},
     'size': {0: 103, 1: 2509, 2: 41939, 3: 145997, 4: 143530},
     'value': {0: 1.5, 1: 4.5, 2: 7.5, 3: 10.5, 4: 13.5}}
)

ax = df.plot(kind='scatter', x='value', y='mean', s=60, c='size', cmap='RdYlGn')

python -  Pandas 缺少X刻度标签-LMLPHP

试图手动添加x刻度标签,但仍然无法正常工作。
ax.set_xticks(df['value'])
ax.set_xticklabels(df['value'])

最佳答案

好的,我认为这是 Pandas 图的错误。但是,此SO post显示以下解决方法。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {'mean': {0: 10,
              1: 16,
              2: 18,
              3: 22,
              4: 30},
     'size': {0: 103, 1: 2509, 2: 41939, 3: 145997, 4: 143530},
     'value': {0: 1.5, 1: 4.5, 2: 7.5, 3: 10.5, 4: 13.5}}
)
fig, ax = plt.subplots()
df.plot(kind='scatter', x='value', y='mean', s=60, c='size', cmap='RdYlGn', ax=ax)

python -  Pandas 缺少X刻度标签-LMLPHP

09-26 21:14