Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 529
Sakurai.JJ Avatar asked Jan 31 '21 08:01

Sakurai.JJ


2 Answers

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

like image 178
hckr Avatar answered Oct 09 '22 19:10

hckr


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,)
like image 1
muffin Avatar answered Oct 09 '22 20:10

muffin