ggplot 和 plotnine 中的自由 y 轴

问题描述

我需要有关 Python 绘图的帮助。我使用了示例数据集 mpg。

import pandas as pd
import numpy as np

from plotnine import *
from plotnine.data import *
import matplotlib.pyplot as plt
%matplotlib inline

mpg.head()
mpg = mpg.loc[(mpg.manufacturer=="audi") | (mpg.manufacturer=="jeep") ]
p = ggplot(aes(x='displ',y='manufacturer'),mpg)

我得到了这个情节:

enter image description here

但我希望形状在 y 轴上是自由的(因为我有重叠的元素),例如这样的东西:

enter image description here

非常感谢!

解决方法

使用 position_jitter 沿垂直方向添加一些随机性。

+ geom_point(position=position_jitter(width=0,height=0.3))