本文介绍了 pandas 缺少X刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在熊猫图中添加c时,x刻度标签消失.有人知道如何将它们重新添加吗?

When I add c to a pandas plot, x tick labels disappear. Does anyone know how to add them back?

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

试图手动添加x个刻度标签,但仍然无法正常工作.

Tried to manually add x tick labels, but still not working.

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

推荐答案

好的,我认为这是熊猫图的错误.但是,此 SO帖子显示了以下解决方法.

Okay, I think this is a bug with pandas plot. However, this SO post shows the following workaround.

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)

这篇关于 pandas 缺少X刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 09:17