pandas 熊猫缺少 x 刻度标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43578976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:27:34 来源:igfitidea点击:
Pandas missing x tick labels
提问by E.K.
When I add c
to a pandas plot, x tick labels disappear. Does anyone know how to add them back?
当我添加c
到Pandas图中时,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')
Tried to manually add x tick labels, but still not working.
尝试手动添加 x 个刻度标签,但仍然无法正常工作。
ax.set_xticks(df['value'])
ax.set_xticklabels(df['value'])
回答by Scott Boston
Okay, I think this is a bug with pandas plot. However, this SO postshows the following workaround.
好的,我认为这是Pandas情节的一个错误。但是,此SO 帖子显示了以下解决方法。
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)