自定义 Python ggplot 的 x 轴刻度?

Customising x-axis ticks for Python ggplot?

之前有人问过这个问题。但就我而言,通过传统方式修改 x 轴刻度并没有产生结果。

我有一个包含超过 12,000 个观察值的数据框,并使用 ggplot 绘制了一个漂亮的时间序列图。

这里的问题是 x 轴刻度。我可以很好地将 date 列转换为正确的 datetime 格式,但这需要额外的代码。我不妨粘贴这些从 1998 年到 2017 年的时间间隔,间隔 5 年,这样就可以了。

但是,在 ggplot 层中,设置轴刻度没有按预期工作。这是完整代码:

plt = ggplot(wyoming_permit_dat, aes(x = 'date', y = 'Total'))

plt + geom_point() + geom_line(color = 'red', alpha = 0.50, size = 2.5) + \
        theme(axis_text_x = element_text(angle = 45, hjust = 1), 
              title = 'Time-Series of Total Checks for Wyoming (1998-2017)', 
              axis_title_x = 'Period', axis_title_y = 'Total Checks') + \
              scale_x_discrete(labels = ['1998', '2003', '2008', '2013', '2018'])

我在为 scale_x_discrete 指定 breaks 参数时可能会出错,但这样做会压缩整个图表并使其看起来像是被压碎了。有没有一种方法可以只修改这些标签而不执行日期时间转换?

所以我想弄清楚如何自己从 R 翻译。到目前为止我想到的最好的方法是手动标记中断和标签。

base = Batting_Salaries[Batting_Salaries['HR'] > 10]
gg = ggplot(base, aes('HR' ,'salary')) + geom_bar() + scale_x_continuous(limits 
= (0,40), breaks = (0,10,20,30,40), labels = [10,20,30,40,50])
gg

HR's on X axis starting at 10