熊猫缺少x刻度标签

时间:2017-04-24 02:43:51

标签: python pandas matplotlib plot

当我将c添加到pandas图中时,x tick标签消失。有谁知道如何添加它们?

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') 

enter image description here

尝试手动添加x刻度标签,但仍无效。

ax.set_xticks(df['value'])
ax.set_xticklabels(df['value'])

1 个答案:

答案 0 :(得分:5)

好的,我认为这是熊猫情节的错误。但是,此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) 

enter image description here