Search code examples
pythonpython-ggplot

Customising x-axis ticks for Python ggplot?


This has been asked before. In my case though, modifying x-axis ticks through the traditional manner is not yielding results.

I have a data frame that has over 12,000 observations and have used ggplot to plot a nice time-series graph.

enter image description here

The problem here is the x-axis ticks. I can well convert the date column to proper datetime format but that will require additional code. I might as well just paste these ticks from 1998 to 2017 with 5 year gaps and that will do the job.

However, in the ggplot layer, setting the axis ticks is not working as expected. This is the full code:

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'])

I might be going wrong at specifying the breaks argument for scale_x_discrete but doing that cramps up the whole graph and makes it look like it got crushed. Is there a way I could just modify these labels without performing date-time conversions?


Solution

  • So I'm trying to figure out how to translate from R myself. The best I've figured out so far is manually labeling the breaks and labels.

    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