Message Boards Message Boards

0
|
5032 Views
|
10 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to add a secondary y axis in a plot with a single curve?

Posted 3 years ago

Hello! I'm trying to add a secondary y axis in a plot. This secondary y axis here is just a function of the primary y axis, but in different units, and needs to be shown only for reference. The set of curves in the plot should be the same referring to the two vertical axes. As can be seen in the image attached, I would like to combine the two vertical axes from the two plots and make it a single plot with left and right axes. I would appreciate any help regarding this.

Please see attached notebook.

![][1]

Attachment

10 Replies

There’s a function in the Function Repository that might do what you want - see MultipleAxesPlot.

POSTED BY: Ian Williams

This function generates two vertical axes, each corresponding to a different curve. I want both the vertical axes to correspond to the same curve.

In any case, suggestion by @Rohit Namjoshi seems to work, although I don't fully understand how that function works.

Thanks again!

Posted 3 years ago

Hi Kaustuh,

Unfortunately Charting`ScaledTicks is an undocumented function. It has been around since version 10, and is widely used internally in WL code, so it is unlikely to be removed any time soon. In it's simplest form it take a list of two functions {f1, f2} where f2 must be the inverse function of f1. f2 takes a single argument, the value to be scaled and returns the scaled value, f1 does the inverse. e.g. in the following plot the x axis ticks are shown as degrees rather than radians.

Plot[Sin[x], {x, 0, Pi},
 Frame -> True,
 FrameTicks -> {Automatic, {Charting`ScaledTicks[{# Degree &, # / Degree &}], Automatic}}]

To see the more complex forms it supports

Needs["GeneralUtilities`"]
PrintDefinitions[Charting`ScaledTicks]
POSTED BY: Rohit Namjoshi

Many thanks for sharing this, Rohit!

Welcome to Wolfram Community!
Please make sure you know the rules: https://wolfr.am/READ-1ST
Please provide your code (formatted) in your post so that other members can copy and evaluate it in order to help you.

Formatted code looks like this

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

You can also embed notebook or attach notebook.

enter image description here

POSTED BY: Moderation Team

Thanks for reminding! I have now attached my notebook.

Posted 3 years ago

Hi Kaustuh,

Use scaled values for the ticks on the right. e.g.

LogLogPlot[x, {x, 10^-7, 1},
 Frame -> True,
 FrameTicks -> {{Automatic, Charting`ScaledTicks[{Log[# 10^-13] &, Exp[#]/10^-13 &}]},
   {Automatic, Automatic}}]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 3 years ago

Hi Kaustuh,

Took a look at the notebook that you have attached now, and the method I proposed should work fine. Have you tried it?

POSTED BY: Rohit Namjoshi

Hi Rohit! Many thanks for your reply! Yes, the method you suggested worked for me. Below is the code edit

FrameTicks -> {{All, 
   Charting`ScaledTicks[{Log[# fOverH/MPlValueGeV] &, 
     Exp[#] MPlValueGeV/fOverH &}]}, {Automatic, None}}

generating the attached plot. Thanks a ton!

I tried searching details about this Charting`ScaledTicks function but couldn't find any documentation. If you have that available, could you please share it? Or if not, could you briefly explain the function arguments? Thanks in advance!

Attachment

Attachments:

Hello Rohit,

great anwer - many thanks for sharing! But where on earth one can find things like Charting'ScaledTicks[...]? My search in the documentation was without any result.

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract