自定义Python ggplot的x轴刻度?

时间:2018-02-14 15:43:11

标签: python python-ggplot

以前曾经问过这个问题。但就我而言,通过传统方式修改x轴刻度不会产生结果。

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

enter image description here

这里的问题是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'])

我在为breaks指定scale_x_discrete参数时可能会出错,但这样做会使整个图形变得痉挛并使其看起来像是被粉碎了。有没有办法在不执行日期时间转换的情况下修改这些标签?

1 个答案:

答案 0 :(得分:1)

所以我试图弄清楚如何从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