Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: format axis offset-values to whole numbers or specific number

I have a matplotlib figure which I am plotting data that is always referred to as nanoseconds (1e-9). On the y-axis, if I have data that is tens of nanoseconds, ie. 44e-9, the value on the axis shows as 4.4 with a +1e-8 as an offset. Is there anyway to force the axis to show 44 with a +1e-9 offset?

The same goes for my x-axis where the axis is showing +5.54478e4, where I would rather it show an offset of +55447 (whole number, no decimal - the value here is in days).

I've tried a couple things like this:

p = axes.plot(x,y) p.ticklabel_format(style='plain') 

for the x-axis, but this doesn't work, though I'm probably using it incorrectly or misinterpreting something from the docs, can someone point me in the correct direction?

Thanks, Jonathan

Problem illustration


I tried doing something with formatters but haven't found any solution yet...:

myyfmt = ScalarFormatter(useOffset=True) myyfmt._set_offset(1e9) axes.get_yaxis().set_major_formatter(myyfmt) 

and

myxfmt = ScalarFormatter(useOffset=True) myxfmt.set_portlimits((-9,5)) axes.get_xaxis().set_major_formatter(myxfmt) 

On a side note, I'm actually confused as to where the 'offset number' object actually resides...is it part of the major/minor ticks?

like image 467
Jonathan Avatar asked Sep 09 '10 14:09

Jonathan


People also ask

What are the methods to adjust axis limits Matplotlib?

By using xlim() and ylim() methods In matplotlib, to set or get X-axis and Y-axis limits, use xlim() and ylim() methods, accordingly. These methods define the limits for respective axes if arguments are passed, and if no arguments are passed, we obtain a range of the respective axes.

What does PLT axis (' equal ') do?

pyplot. axis() , it creates a square plot where the ranges for both axes occupy are equal to the length in plot.

What is the difference between axis and axes in Matplotlib?

Axes is the region where the plots and graphs are drawn. Each axes has its own title, x-axis and y-axis. On the other hand, the axis in Matplotlib refers to the scale of the graph. A two-dimensional graph has an x-axis and a y-axis, whereas a three-dimensional graph has an x-axis, a y-axis, and a z-axis.


2 Answers

I had exactly the same problem, and these lines fixed the problem:

from matplotlib.ticker import ScalarFormatter  y_formatter = ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) 
like image 92
Gonzalo Avatar answered Sep 18 '22 06:09

Gonzalo


A much easier solution is to simply customize the tick labels. Take this example:

from pylab import *  # Generate some random data... x = linspace(55478, 55486, 100) y = random(100) - 0.5 y = cumsum(y) y -= y.min() y *= 1e-8  # plot plot(x,y)  # xticks locs,labels = xticks() xticks(locs, map(lambda x: "%g" % x, locs))  # ytikcs locs,labels = yticks() yticks(locs, map(lambda x: "%.1f" % x, locs*1e9)) ylabel('microseconds (1E-9)')  show() 

alt text

Notice how in the y-axis case, I multiplied the values by 1e9 then mentioned that constant in the y-label


EDIT

Another option is to fake the exponent multiplier by manually adding its text to the top of the plot:

locs,labels = yticks() yticks(locs, map(lambda x: "%.1f" % x, locs*1e9)) text(0.0, 1.01, '1e-9', fontsize=10, transform = gca().transAxes) 

EDIT2

Also you can format the x-axis offset value in the same manner:

locs,labels = xticks() xticks(locs, map(lambda x: "%g" % x, locs-min(locs))) text(0.92, -0.07, "+%g" % min(locs), fontsize=10, transform = gca().transAxes) 

alt text

like image 36
Amro Avatar answered Sep 18 '22 06:09

Amro