TransWikia.com

Control the display unit length of `PlotRange`

Mathematica Asked on July 8, 2021

If I have an example plotted data such as:

ListPlot[{{-20, 2}, {-10, 1}, {10, 0.001}, {20, 0.0000001}, {80, 0.000000001}}, PlotRange -> {{-20, 80}, All}] 

Which plotted gives:

enter image description here

How can I use PlotRange to display the x-axis every 10 units rather than every 20 units as it is by default?

One Answer

Here I use the undocumented Charting`ScaledTicks function:

ListPlot[
 {{-20, 2}, {-10, 1}, {10, 0.001}, {20, 0.0000001}, {80, 0.000000001}},
 PlotRange -> {{-20, 80}, All},
 Ticks -> {
    Charting`ScaledTicks[{Identity, Identity}][-20, 80, {10, 2}], 
    Automatic}
]

plot

What I know about this function I've mostly learned in the comments to this answer. In short:

Charting`ScaledTicks[{scalingFunction, inverseScalingFunction}][min, max, divisions]

where divisions can be a number, or {maximum number of major ticks, number of divisions between each pair of major ticks}, so the specification I used above asks for labeled major ticks every 10 units, with one minor tick in between each major one (i.e., two divisions between each major tick). Note that sometimes the behavior of divisions is quirky; I assume that it uses FindDivisions or similar behind the scenes, which always attempts to find "pretty" / "good" divisions according to a heuristic.

Correct answer by MarcoB on July 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP