Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Y Axes sharing the same X Axis? #25

Open
evdubs opened this issue Dec 30, 2017 · 9 comments
Open

Multiple Y Axes sharing the same X Axis? #25

evdubs opened this issue Dec 30, 2017 · 9 comments

Comments

@evdubs
Copy link
Contributor

evdubs commented Dec 30, 2017

Hello,

I would like to know if there is a way to have two datasets share an x-axis while using independent y-axes. My use case is to plot stock prices using a price axis and volumes or forward earnings on the same plot
so that they can all be viewed together. Currently, I have the following built where volume rectangles are just drawn on a line near the prices, but the values are obscured this way. I have also tried to look at just having two independent plots, but the x-axis in one does not perfectly align with the other.

Is there a way to do this?

@jbclements
Copy link

jbclements commented Dec 30, 2017 via email

@evdubs
Copy link
Contributor Author

evdubs commented Dec 30, 2017

I noticed that there was a near-y-axis and far-y-axis notion in the documentation. However, this seems limited by just showing something like the decimal 1.0 or a currency $1.0. I want to have these axes scaled differently so that a y-axis shows prices from $50-$70 and the volumes can be shown on a separate, simultaneous y-axis that is, say, scaled from 0 million - 10 million with most of the rectangles at around 1 million (so that it is just in the bottom of the graph). Does this make sense?

@evdubs
Copy link
Contributor Author

evdubs commented Dec 30, 2017

Here is a separate example of what I am trying to achieve. There are two separate data series shown with different y values but they share the same x-axis. Plotting this in racket/plot would instead have the blue line below at 0-11 and the red line way above at 36-52.

@bennn
Copy link
Contributor

bennn commented Dec 31, 2017

I think you get what you want by setting plot-y-ticks and plot-y-far-ticks to your own ticks values (with similar layout fields and different format fields).

Here's an example --- let me know if this doesn't make sense. The one really strange thing (IMO) is that the near and far axis need to plot the same number of major ticks.

#lang racket/base

(require
  plot/no-gui
  (only-in racket/list range take)
  (only-in racket/format ~a))

(define (make-custom-ticks positions labels)
  (define (ticks-layout ax-min ax-max)
    (for/list ((n (in-list positions)))
      (pre-tick n #true)))
  (define (ticks-format ax-min ax-max pre-ticks)
    (for/list ([pt (in-list pre-ticks)]
               [lbl (in-list labels)])
      (~a lbl)))
  (ticks ticks-layout ticks-format))

(module+ main
  (parameterize ([plot-y-ticks
                  (make-custom-ticks (range 0 10) (range 34 54 2))]
                 [plot-y-far-ticks
                  (make-custom-ticks (take (range 0 10 0.8) 10) (range 0 10))])
    (plot-file
      (list
        (function values)
        (hrule 0 #:color 0)
      )
      "test.png"
      #:width 100
      #:x-min -0 #:x-max 10
      #:y-min -2 #:y-max 10)))
;; output attached

multiple-y

@evdubs
Copy link
Contributor Author

evdubs commented Dec 31, 2017

@bennn thanks for the example. It looks like I will still need to try to normalize the data first so that it fits together and then change the ticks to fit the data. I think that will be workable, but of course it would be nice, I think, to just be able to dump the data as it is to racket/plot and have it figure out what the two different axes ticks should be (as in the Age Wave & Inflation chart).

@rfindler
Copy link
Member

rfindler commented Jan 2, 2018

@rfindler
Copy link
Member

rfindler commented Jan 2, 2018

Hm. Maybe not. It seems like what you want is to have something like plot-x-transform, but that applies to only some of the content of the plot, not all of it. Sorry!

@alex-hhh
Copy link
Sponsor Collaborator

This would be a nice to have feature. I can currently produce dual axis plots that look OK, but this is complicated, as I have to scale the secondary data series to be in the same range as the primary data series and this requires knowing the dimensions of the plot (i.e. min and max values).

best-avg-1945-gap-hr

@stefanosnikolaou
Copy link

Hello,

Is it possible to add a 3rd y-axis?
For example, something similar with this matplotlib example -> https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html where there are 3 y-axes with names Density, Temperature and Velocity.

The ticks-scale example with the Celcius and Fahrenheit was very helpful and I was able to use it to make a similar plot like the one alex-hhh of the previous post shows, but now my problem requires a third different scaling and I cannot find how to do this with racket/plot.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants