Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the y axis in radians in a Python plot?

I would like to write the radian units of the axes as proportional to \pi: something like

$\frac{\pi}{4}$, $\frac{\pi}{2}$, ... 

in place of

0.785, 1.5707 ... 

Is there any standard way? As an example, what should I add to the following code?

from pylab import *

x=arange(-10.0,10.0,0.1)
y= arctan(x)

plot(x,y,'b.')
show()

I found this example http://matplotlib.sourceforge.net/examples/units/radian_demo.html but it does not work because I don't have basic_units module.

Thank you!

like image 922
DdD Avatar asked May 24 '12 00:05

DdD


People also ask

How do you change the Y axis labels in python?

To set labels on the x-axis and y-axis, use the plt. xlabel() and plt. ylabel() methods.


1 Answers

You can download the basic_units.py file here:

After it should work like this:

from pylab import *
from basic_units import radians

x = arange(-10.0,10.0,0.1)
y = map(lambda y: y*radians,arctan(x))
x = map(lambda x: x*radians,x)

plot(x,y,'b.',xunits=radians,yunits=radians)
show()

Alternatively you could implement the arctan function the way they implemented the cos function in basic_units.py

like image 186
F-A Avatar answered Oct 20 '22 13:10

F-A