Home > front end >  Axis scaling with plotnine
Axis scaling with plotnine

Time:10-20

Until now I've spend several hours to get axis scaling in plotnine done. Everything I've found until now does not work.

Is it the data, is it the code defining the graph, hope any of you would be able to see what is wrong.

The data looks like:

enter image description here

Data types are:

concat                 object
direction              object
date_trunc    timedelta64[ns]
comment                object
timestamp      datetime64[ns]
dtype: object

The following code creates a graph as expected:

ch_time_plot = (p9.ggplot(data= ch_time,
                         mapping= p9.aes(x= 'timestamp', 
                                         y= 'date_trunc',
                                         color= 'direction'))
                  p9.geom_line()
                  p9.theme(axis_text_x= p9.element_text(rotation=90))
               )

# ch_time_plot  = p9.ylim(0,5)
# => TypeError: Discrete value supplied to continuous scale

# ch_time_plot  = p9.expand_limits(y=0)
# => AttributeError: 'int' object has no attribute 'value'

# ch_time_plot  = p9.scale_y_continuous(limits = (1, 10))
# => TypeError: Discrete value supplied to continuous scale

ch_time_plot

What I want to achieve is that always the origin of the y axis (0) is shown. As soon I comment out one of the lines shown in the code (which should do this) I get an error message and no more graph.

What is wrong with my code?

CodePudding user response:

Scaling in the example above did not work due to the data types used.

As long on the y-axis timedelta64[ns] is used scaling did not work.

After the following conversion:

ch_time['change_time']= ch_time['date_trunc'] / np.timedelta64(1, 's')

The graph can properly scaled with:

ch_time_plot = (p9.ggplot(data= ch_time,
                         mapping= p9.aes(x= 'timestamp', 
                                         y= 'change_time',
                                         color= 'direction'))
                  p9.geom_line()
                  p9.theme(axis_text_x= p9.element_text(rotation=90))
                  p9.scale_y_continuous(limits=(0, 6))
               )
  • Related