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

标签 python matplotlib ggplot2 scatter python-ggplot

以下示例返回错误。似乎不支持在 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/

相关文章:

python - 在 Django 中中止 Celery 中正在运行的任务

python - Hook Python 标准库模块

python - 如何在matplotlib中绘制从训练集的每个点到曲面的垂直线

r - 在 ggplot 中的何处以及如何应用过滤器

r - 函数 : when does it not recognize arguments and when does it? 内的 ggplot

python - 如何计算以字符串开头的单词数

python - 将一列的值分成两列

python - 使用 python 动态地将 matplotlib 图像提供给网络

python - 如何在 Python 中将 xaxis 网格放置在频谱图上?

r - 具有重复观察的 ggplot 熔解数据