Mathematica: How to use two x-axis with different units?

In summary, Karl is trying to combine two ListPlot objects, but they end up showing below each other.
  • #1
karl21
2
0
Hello,

I've collected data with pairs of { resolution, filesize in bit }.

Now I would like to use a ListPlot to visualize the data in mathematica.
My problem is now showing the x-asis two times. One for the filesize in bit (above) and the other for the filesize in KiB (below).

Code:
ListPlot[
    {{35640,36000000},{62784,64000000},{97704,100000000}},
    FrameLabel->{"KiB","Resolution (px)", "Bit"},
    Frame->True,
    GridLines->Automatic,
    AxesOrigin->{1,0},
    GridLinesStyle->{{Gray, Dotted}, {Gray, Dotted}},
    FrameTicks->{{All,None},{All,All}}
]

I know, I need to change the second 'all' to something like "take the x-axis value and divide it by 1024".

mathematica_x.png


Can anyone help me out please how to solve this?

Thanks in advance
Karl
 
Physics news on Phys.org
  • #2
Here's a quick trick - you can plot the KiB one with
Code:
plot1=ListPlot[{#1/1024, #2} & @@@ {{35640, 36000000}, {62784,
  64000000}, {97704, 100000000}},
 FrameLabel -> {"KiB", "Resolution (px)"}, Frame -> True,
 GridLines -> Automatic, AxesOrigin -> {1, 0},
 GridLinesStyle -> {{Gray, Dotted}, {Gray, Dotted}},
 FrameTicks -> {{All, None}, {All, None}}, ImagePadding -> 60,
 ImageSize -> 800, PlotRange -> {{0, Automatic}, Automatic}]

The "#" part is just dividing the first element in each list with 1024. Now, the "Bit" one would be:
Code:
plot2=ListPlot[{{35640, 36000000}, {62784, 64000000}, {97704, 100000000}},
 FrameLabel -> {None, "Resolution (px)", "Bit"}, Frame -> True,
 ImagePadding -> 60, GridLines -> Automatic, AxesOrigin -> {1, 0},
 GridLinesStyle -> {{Gray, Dotted}, {Gray, Dotted}},
 FrameTicks -> {{All, None}, {None, All}}, ImageSize -> 800,
 PlotRange -> {{0, Automatic}, Automatic}]

Then simply use Overlay[{plot1,plot2}] and you'll get what you were looking for. Note: the ImagePadding is crucial, without it the Overlay won't work as it should.
 
  • #3
Hello,

thanks for your replay.
However it still doesn't work :(

I can separately create the plots - with correct axis.
But when I use Overlay the 2 plots simply get shown below each other - not combined.

Best regards
Karl
 
  • #4
Works fine on MMA 10, which version are you using? I can't really think of any other way to do this if Overlay doesn't work, I don't think it's possible to do both using just one ListPlot.
 

Related to Mathematica: How to use two x-axis with different units?

1. How can I add a second x-axis with different units in Mathematica?

You can use the "FrameTicks" option in the "Plot" function to specify the tick marks and labels for the second x-axis. For example, "FrameTicks -> {{Automatic, None}, {myTicks, myLabels}}" will add a second x-axis with custom tick marks and labels specified by the lists "myTicks" and "myLabels".

2. Can I use different scales for the two x-axes?

Yes, you can use the "ScalingFunctions" option in the "Plot" function to specify different scaling functions for each x-axis. For example, "ScalingFunctions -> {{Log, None}, {None, None}}" will use a logarithmic scale for the first x-axis and a linear scale for the second x-axis.

3. How do I change the appearance of the second x-axis?

You can use the "FrameStyle" option in the "Plot" function to change the style of the second x-axis. For example, "FrameStyle -> Directive[Red, Thick]" will make the second x-axis appear in red and with a thicker line.

4. Can I add a label to the second x-axis?

Yes, you can use the "FrameLabel" option in the "Plot" function to add a label to the second x-axis. For example, "FrameLabel -> {"x (unit 1)", "x (unit 2)"}" will label the first x-axis as "x (unit 1)" and the second x-axis as "x (unit 2)".

5. Is it possible to use a different data set for the second x-axis?

Yes, you can use the "Plot" function to plot multiple data sets on the same graph, with each data set corresponding to a different x-axis. You can then use the "FrameTicks" and "FrameLabel" options to customize the second x-axis accordingly.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
944
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Differential Equations
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
457
Back
Top