JetBrains / lets-plot-kotlin

Grammar of Graphics for Kotlin
https://lets-plot.org/kotlin/
MIT License
430 stars 36 forks source link

How to plot TimeSeries? #37

Closed wangtieqiao closed 2 years ago

wangtieqiao commented 4 years ago

Is there any example notebook to show how to plot timeseries data with time in X-axis?

X-axis data could be any of the Datetime classes like LocalDate, LocalDateTime or ZonedDateTime etc

alshan commented 4 years ago

Try using scale_x_datetime.

At the moment this is the only way to plot time series. The input series can contain millis since Unix epoch. Also accepted Date, Instant and ZonedDateTime from java.time but these values will be converted to millis since Unix epoch.

LocalDate, LocalTime and LocalDateTime are not supported.

When formatting date-time labels Lets-plot assumes UTC time-zone.

wangtieqiao commented 4 years ago

Thanks @alshan that works great!

Another question:

alshan commented 4 years ago

Glad it worked for you. Regarding the other questions:

To add a title use the ggtitle function.

The background gridlines are not there yet but you can generate and style a set of horizontal/vertical lines using geom_hline and geom_vline functions.

We don't have the zoom/pan kind of interactivity. The closest thing could be using the xlim or ylim parameters in coord_xxx function (coord_cartesian for example). Perhaps, if ipywidgets are available, a slider can be used to set these values but nobody ever tried this :).

wangtieqiao commented 4 years ago

Thanks! great work team! Any plan add those features in?

alshan commented 4 years ago

Yes for gridlines, panning/zooming not in the nearest future)

wangtieqiao commented 4 years ago

Thanks again! a bit sad to know that zoom/pan will not be available soon.... I am a quant researcher / data scientist dealing with timesries everyday, panning/zooming is essential for my work. I see Kotlin starting to take off in analytic area, but could not find any charting library can replace what I used to in Python. Have tried BeakerX, but their wire to Kotlin kernel is broken. Anyway great work team, can not wait to see more features get into the library.

alshan commented 4 years ago

Thanks for the feedback! You can try the plotly.kt - not quite sure but it might have panning/zooming.

alshan commented 3 years ago

Is there any example notebook to show how to plot timeseries data with time in X-axis?

X-axis data could be any of the Datetime classes like LocalDate, LocalDateTime or ZonedDateTime etc

There is a new demo in 1.3.0 with time series on X-axis: https://nbviewer.jupyter.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/examples/jupyter-notebooks/formatting_axes_etc.ipynb

loehnertz commented 3 years ago

@wangtieqiao I made myself this method that works pretty well for the grid lines:

    fun hlines(range: IntProgression): Feature {
        val horizontalLine: (Int) -> Feature = { yIntercept: Int ->
            geom_hline(
                yintercept = yIntercept,
                alpha = 0.1,
                color = GENERAL_MUTED_COLOR,
                size = 0.02,
            )
        }

        var horizontalLines: Feature = horizontalLine(range.first)
        for (yIntercept: Int in range.first + range.step until range.last step range.step) {
            horizontalLines += horizontalLine(yIntercept)
        }

        return horizontalLines
    }

It can be used like this:

ggplot(dataFrame) + hlines(-110 until 110 step 5)

Cheers 😊