gpt4 book ai didi

python - 在用于 Python 的 ggplot 中,将离散 X 标度与 geom_point() 一起使用?

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:54 25 4
gpt4 key购买 nike

以下示例返回错误。似乎不支持在 Python 的 ggplot 中对 x 轴使用离散(非连续)刻度?

import pandas as pd
import ggplot

df = pd.DataFrame.from_dict({'a':['a','b','c'],
'percentage':[.1,.2,.3]})

p = ggplot.ggplot(data=df,
aesthetics=ggplot.aes(x='a',
y='percentage'))\
+ ggplot.geom_point()

print(p)

如前所述,这将返回:

Traceback (most recent call last):
File "/Users/me/Library/Preferences/PyCharm2016.1/scratches/scratch_1.py", line 30, in <module>
print(p)
File "/Users/me/lib/python3.5/site-packages/ggplot/ggplot.py", line 116, in __repr__
self.make()
File "/Users/me/lib/python3.5/site-packages/ggplot/ggplot.py", line 627, in make
layer.plot(ax, facetgroup, self._aes, **kwargs)
File "/Users/me/lib/python3.5/site-packages/ggplot/geoms/geom_point.py", line 60, in plot
ax.scatter(x, y, **params)
File "/Users/me/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner
return func(ax, *args, **kwargs)
File "/Users/me/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3838, in scatter
x, y, s, c = cbook.delete_masked_points(x, y, s, c)
File "/Users/me/lib/python3.5/site-packages/matplotlib/cbook.py", line 1848, in delete_masked_points
raise ValueError("First argument must be a sequence")
ValueError: First argument must be a sequence

在离散尺度上使用 ggplot 散点有任何解决方法吗?

最佳答案

一种选择是生成连续序列,并使用原始变量作为标签。但这似乎是一个痛苦的解决方法。

df = pd.DataFrame.from_dict( {'a':[0,1,2],
'a_name':['a','b','c'],
'percentage':[.1,.2,.3]})

p = ggplot.ggplot(data=df,
aesthetics=ggplot.aes(x='a',
y='percentage'))\
+ ggplot.geom_point()\
+ ggplot.scale_x_continuous(breaks=list(df['a']),
labels=list(df['a_name']))

关于python - 在用于 Python 的 ggplot 中,将离散 X 标度与 geom_point() 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39460617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com