How can I make scientific y-ticks in Julia plots?
Asked Answered
H

2

8

I use Plots in Julia 1.5. How can I make the y-ticks as shown below?

We sometime see figure with the right type of axis in scientific paper.

enter image description here

Hightest answered 31/1, 2021 at 8:32 Comment(0)
A
6

I do not know if there is an easy and a quick way but here is my solution.

First we should arrange tick labels. Plots.jl provides us with formatter axis attribute. We can set this attribute to a function that takes the number value of the tick and returns a string as the tick label.

x=0:0.2:0.8
y=[1,2,3,4,5] * 10^-8    
plot(x, y, ytick=y, yaxis=(formatter=y->string(round(Int, y / 10^-8))))

To annotate the axis with the correct scale, I use Plots.annotate! and Plots.text, and set the location and size of the annotation appropriately. The problem with this solution is that the annotation may overflow the plot area and may not be entirely visible if there is no title set. You may set a phantom title if such a problem occurs.

using LaTeXStrings    
x = 0:0.2:0.8
y = [1,2,3,4,5] * 10^-8
plot(x, y, yticks = y,  yaxis=(formatter=y->string(round(Int, y / 10^-8))), title="   ")
annotate!([(0, maximum(y) * 1.05, Plots.text(L"\times10^{-8}", 11, :black, :center))])

Note that I handled the conversion from numeric value to scientific notation manually. You may handle it automatically using tools from Printf module.

Here is the final result

Final result with scientific xtick

Anile answered 31/1, 2021 at 13:8 Comment(0)
A
3

Just add formatter = :plain to your plot function:

plot(x, y, formatter = :plain,)

formatter = identity is also an option (for displaying numbers with decimal point):

plot(x, y, formatter = identity,)
Alric answered 19/8, 2021 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.