I want to create such figure (https://lh3.googleusercontent.com/-DCFuRHtqTmk/VFq_cf0Cj4I/AAAAAAAAETA/hDPE8N8LLDE/w682-h695-no/figure.png) using PLT.MATSHOW with labeled majorticks (in range from 0 to 20 with step = 1), unlabeled minorticks (from 0.5 to 18.5 with step = 1) and minorgrids.
Here is my code, but something is wrong with it (it draws more lines than I need and I do not know how to change number of minorticks):
import numpy as np
import numpy.random as random
import matplotlib.pyplot as plt
S0 = np.ones([20,20], int)
S = np.copy(S0)
Mx = np.shape(S)[0]
My = np.shape(S)[1]
for x in range(Mx):
for y in range(My):
S[x,y]=2*random.randint(2)-1
plt.matshow(S, fignum = None, alpha = 0.75, cmap = "summer")
plt.xticks(range(0, Mx, 1))
plt.yticks([i for i in range(0, My)])
plt.grid(which = 'minor', ls = '-')
plt.minorticks_on()
plt.show()
How should I rewrite it to solve my problem?
Thank you for the help! (:
I believe this solves your problem.
import numpy as np
import numpy.random as random
import matplotlib.pyplot as plt
S0 = np.ones([20,20], int)
S = np.copy(S0)
Mx = np.shape(S)[0]
My = np.shape(S)[1]
for x in range(Mx):
for y in range(My):
S[x,y]=2*random.randint(2)-1
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.matshow(S,alpha=0.75, cmap="summer", interpolation="none")
ax.set_xticks([i+0.5 for i in range(Mx)])
ax.set_yticks([i+0.5 for i in range(My)])
ax.set_xticklabels(range(Mx))
ax.set_yticklabels(range(My))
#plt.grid(which = 'minor', ls = '-')
plt.grid(which = 'major', ls = '-')
plt.minorticks_on()
plt.show()
The changes that i have made are threefold; the first replaces the grid with the major grid lines, and the second is that i have moved the ticks and corresponding labels by 0.5, so that they match the boundaries. The third is to remove interpolation in the matshow command. This all gives this:
I have finally found how to solve the problem and get what I want. Thank you, Will, for the help!
If someone else knows how to get the final result using a different method, I would appretiate if you let me know.
Here is new code:
import numpy as np
import numpy.random as random
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, AutoMinorLocator
S0 = np.ones([20,20], int)
S = np.copy(S0)
Mx = np.shape(S)[0]
My = np.shape(S)[1]
for x in range(Mx):
for y in range(My):
S[x,y]=2*random.randint(2)-1
fig = plt.figure()
ax = fig.add_subplot(111)
ax.matshow(S, alpha = .75, cmap = "summer")
ax.set_xticklabels(range(-1,Mx))
ax.set_yticklabels(range(-1,My))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
ax.xaxis.grid(True,'minor', lw = 1.5, ls = '-')
ax.yaxis.grid(True,'minor', lw = 1.5, ls = '-')
plt.show()
Related
I'm trying to scale the y-axis so my errorbars can be seen.
Any help would be appreciated! :)
Here is my current code.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline
x = ntermsList
y = allPmuCycleCountAverages
xerr = 0
yerr = allPmuCycleCountStandardDeviations
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=xerr, yerr=yerr,fmt='-o')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_title('Line plot with error bars')
ax.set_xticks(ntermsList)
ax.set_xticklabels(ntermsList)
ax.set_yticks(allPmuCycleCountAverages)
ax.yaxis.grid(True)
plt.show()
I've tried these solutions, but no joy:
plt.ylim(-1, 1)
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.yticks(np.arange(min(y), max(y)+0.5, 0.01))
I was expecting the y-axis scale to zoom close enough to the points so my errorbars could be seen
Try autoscalling based in y ticks. Here I'm adding some logic that just rescales the y-axis based on the data that is in the visible x-region. As I don't have your data I took random data.
import numpy as np
import random
ntermsList = np.random.randint(low=0, high=10, size=(555,))
allPmuCycleCountAverages = np.random.randint(low=0, high=10, size=(555,))
allPmuCycleCountStandardDeviations = np.random.randint(low=0, high=10, size=(555,))
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline
x = ntermsList
y = allPmuCycleCountAverages
xerr = 0
yerr = allPmuCycleCountStandardDeviations
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=xerr, yerr=yerr,fmt='-o')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_title('Line plot with error bars')
ax.set_xticks(ntermsList)
ax.set_xticklabels(ntermsList)
ax.set_yticks(allPmuCycleCountAverages)
#plt.setp(ax.get_yticklabels(), rotation=90, horizontalalignment='right')
ax.yaxis.grid(True)
margin =0.1
def get_bottom_top(line):
xd = line.get_xdata()
yd = line.get_ydata()
lo,hi = ax.get_xlim()
y_displayed = yd[((xd>lo) & (xd<hi))]
h = np.max(y_displayed) - np.min(y_displayed)
bot = np.min(y_displayed)-margin*h
top = np.max(y_displayed)+margin*h
return bot,top
lines = ax.get_lines()
bot,top = np.inf, -np.inf
for line in lines:
new_bot, new_top = get_bottom_top(line)
if new_bot < bot: bot = new_bot
if new_top > top: top = new_top
ax.set_ylim(bot,top)
plt.show()
Before Rescalling
After rescalling
I want arrows next to a curve. For example:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0,4*np.pi,10000)
Y = np.sin(X)
shift = 0.1
seg_size = 300
i = 0
plt.plot(X,Y,color='blue')
while i +seg_size < len(X):
x = X[i:i+seg_size]
y = Y[i:i+seg_size]+shift
plt.plot(x,y,color='black')
#input here command for arrow head
i += seg_size*2
plt.show()
I tried to calculate the angle next of the line at the end of the curve and plot the arrow head lines, but I'm doing something wrong and the arrow head are deformed. Any hints?
The FancyArrowPatch class takes a path as an argument, so I thought that you could use that.
1) For each line segment, create a matplotlib.path.Path instance.
2) Use path instance to draw arrow.
import numpy as np
import matplotlib.pyplot as plt; plt.ion()
from matplotlib.patches import FancyArrowPatch, PathPatch
from matplotlib.path import Path
def create_path(x,y):
vertices = zip(x,y)
codes = [Path.MOVETO] + (len(vertices)-1) * [Path.CURVE3]
return Path(vertices, codes)
X = np.linspace(0,4*np.pi,10000)
Y = np.sin(X)
fig, ax = plt.subplots(1,1)
ax.plot(X,Y,color='blue')
shift = 0.1
seg_size = 300
i = 0
while i +seg_size < len(X):
x = X[i:i+seg_size]
y = Y[i:i+seg_size]+shift
path = create_path(x,y)
# for testing path
# patch = PathPatch(path, facecolor='none', lw=2)
# ax.add_patch(patch)
arrow = FancyArrowPatch(path=path, color='r')
ax.add_artist(arrow)
i += seg_size*2
Unfortunately, that does not work, as the path that is passed to FancyArrowPatch cannot have more than 2 segments (not documented, but there is a check in ensure_quadratic_bezier).
So you have to cheat. Below I use the last 2 points of each segment to draw the arrow.
import numpy as np
import matplotlib.pyplot as plt; plt.ion()
from matplotlib.patches import FancyArrowPatch
X = np.linspace(0,4*np.pi,10000)
Y = np.sin(X)
fig, ax = plt.subplots(1,1)
ax.plot(X,Y,color='blue')
shift = 0.1
seg_size = 300
i = 0
while i +seg_size < len(X):
x = X[i:i+seg_size]
y = Y[i:i+seg_size]+shift
ax.plot(x, y, 'k')
posA, posB = zip(x[-2:], y[-2:])
edge_width = 2.
arrowstyle = "fancy,head_length={},head_width={},tail_width={}".format(2*edge_width, 3*edge_width, edge_width)
arrow = FancyArrowPatch(posA=posA, posB=posB, arrowstyle=arrowstyle, color='k')
ax.add_artist(arrow)
i += seg_size*2
How about using the
numpy.annotate()
function?
See examples of arrows created by the pyplot.annotate() function here:
https://matplotlib.org/examples/pylab_examples/annotation_demo.html
I'd like to make a plot in Python and have x range display ticks in multiples of pi.
Is there a good way to do this, not manually?
I'm thinking of using matplotlib, but other options are fine.
EDIT 3: EL_DON's solution worked for me like this:
import matplotlib.ticker as tck
import matplotlib.pyplot as plt
import numpy as np
f,ax=plt.subplots(figsize=(20,10))
x=np.linspace(-10*np.pi, 10*np.pi,1000)
y=np.sin(x)
ax.plot(x/np.pi,y)
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=1.0))
plt.style.use("ggplot")
plt.show()
giving:
EDIT 2 (solved in EDIT 3!): EL_DON's answer doesn't seem to work right for me:
import matplotlib.ticker as tck
import matplotlib.pyplot as plt
import numpy as np
f,ax=plt.subplots(figsize=(20,10))
x=np.linspace(-10*np.pi, 10*np.pi)
y=np.sin(x)
ax.plot(x/np.pi,y)
ax.xaxis.set_major_formatter(tck.FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_locator(tck.MultipleLocator(base=1.0))
plt.style.use("ggplot")
plt.show()
gives me
which really doesn't look right
This is inspired by Python Data Science Handbook, although Sage attempts to do without explicit parameters.
EDIT: I've generalized this to allow you to supply as optional parameters the denominator, the value of the unit, and the LaTeX label for the unit. A class definition is included if you find that helpful.
import numpy as np
import matplotlib.pyplot as plt
def multiple_formatter(denominator=2, number=np.pi, latex='\pi'):
def gcd(a, b):
while b:
a, b = b, a%b
return a
def _multiple_formatter(x, pos):
den = denominator
num = np.int(np.rint(den*x/number))
com = gcd(num,den)
(num,den) = (int(num/com),int(den/com))
if den==1:
if num==0:
return r'$0$'
if num==1:
return r'$%s$'%latex
elif num==-1:
return r'$-%s$'%latex
else:
return r'$%s%s$'%(num,latex)
else:
if num==1:
return r'$\frac{%s}{%s}$'%(latex,den)
elif num==-1:
return r'$\frac{-%s}{%s}$'%(latex,den)
else:
return r'$\frac{%s%s}{%s}$'%(num,latex,den)
return _multiple_formatter
class Multiple:
def __init__(self, denominator=2, number=np.pi, latex='\pi'):
self.denominator = denominator
self.number = number
self.latex = latex
def locator(self):
return plt.MultipleLocator(self.number / self.denominator)
def formatter(self):
return plt.FuncFormatter(multiple_formatter(self.denominator, self.number, self.latex))
This can be used very simply, without any parameters:
x = np.linspace(-np.pi, 3*np.pi,500)
plt.plot(x, np.cos(x))
plt.title(r'Multiples of $\pi$')
ax = plt.gca()
ax.grid(True)
ax.set_aspect(1.0)
ax.axhline(0, color='black', lw=2)
ax.axvline(0, color='black', lw=2)
ax.xaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
ax.xaxis.set_minor_locator(plt.MultipleLocator(np.pi / 12))
ax.xaxis.set_major_formatter(plt.FuncFormatter(multiple_formatter()))
plt.show()
Or it can be used in a more sophisticated way:
tau = np.pi*2
den = 60
major = Multiple(den, tau, r'\tau')
minor = Multiple(den*4, tau, r'\tau')
x = np.linspace(-tau/60, tau*8/60,500)
plt.plot(x, np.exp(-x)*np.cos(60*x))
plt.title(r'Multiples of $\tau$')
ax = plt.gca()
ax.grid(True)
ax.axhline(0, color='black', lw=2)
ax.axvline(0, color='black', lw=2)
ax.xaxis.set_major_locator(major.locator())
ax.xaxis.set_minor_locator(minor.locator())
ax.xaxis.set_major_formatter(major.formatter())
plt.show()
f,ax=plt.subplots(1)
x=linspace(0,3*pi,1001)
y=sin(x)
ax.plot(x/pi,y)
ax.xaxis.set_major_formatter(FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(base=1.0))
I used info from these answers:
https://stackoverflow.com/a/19972993/6605826
https://stackoverflow.com/a/29188910/6605826
If you want to avoid dividing x by pi in the plot command, this answer can be adjusted slightly using a FuncFormatter instead of a FormatStrFormatter:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter, MultipleLocator
fig,ax = plt.subplots()
x = np.linspace(-5*np.pi,5*np.pi,100)
y = np.sin(x)/x
ax.plot(x,y)
#ax.xaxis.set_major_formatter(FormatStrFormatter('%g $\pi$'))
ax.xaxis.set_major_formatter(FuncFormatter(
lambda val,pos: '{:.0g}$\pi$'.format(val/np.pi) if val !=0 else '0'
))
ax.xaxis.set_major_locator(MultipleLocator(base=np.pi))
plt.show()
gives the following image:
Solution for pi fractions:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text', usetex=True) # Use LaTeX font
import seaborn as sns
sns.set(color_codes=True)
Plot your function:
fig, ax = plt.subplots(1)
x = np.linspace(0, 2*np.pi, 1001)
y = np.cos(x)
ax.plot(x, y)
plt.xlim(0, 2*np.pi)
Modify the range of the grid so that it corresponds to the pi values:
ax.set_xticks(np.arange(0, 2*np.pi+0.01, np.pi/4))
Change axis labels:
labels = ['$0$', r'$\pi/4$', r'$\pi/2$', r'$3\pi/4$', r'$\pi$',
r'$5\pi/4$', r'$3\pi/2$', r'$7\pi/4$', r'$2\pi$']
ax.set_xticklabels(labels)
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,3*np.pi,1001)
plt.ylim(-3,3)
plt.xlim(0, 4*np.pi)
plt.plot(x, np.sin(x))
tick_pos= [0, np.pi , 2*np.pi]
labels = ['0', '$\pi$', '$2\pi$']
plt.xticks(tick_pos, labels)
I created a PyPi Package that creates formatter and locator instances like Scott Centoni's answer.
"""Show a simple example of using MultiplePi."""
import matplotlib.pyplot as plt
import numpy as np
from matplot_fmt_pi import MultiplePi
fig = plt.figure(figsize=(4*np.pi, 2.4))
axes = fig.add_subplot(111)
x = np.linspace(-2*np.pi, 2*np.pi, 512)
axes.plot(x, np.sin(x))
axes.grid(True)
axes.axhline(0, color='black', lw=2)
axes.axvline(0, color='black', lw=2)
axes.set_title("MultiplePi formatting")
pi_manager = MultiplePi(2)
axes.xaxis.set_major_locator(pi_manager.locator())
axes.xaxis.set_major_formatter(pi_manager.formatter())
plt.tight_layout()
plt.savefig("./pi_graph.png", dpi=120)
Here is a version converting floats into fractions of pi. Just use your favorite formatter, then convert the float values it produced into pi fractions using function convert_to_pi_fractions(ax, axis='x'), specifying which spine must be converted (or both). You get that:
from that:
from fractions import Fraction
import numpy as np
from numpy import pi
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
def convert_to_pi_fractions(ax, axis='x'):
assert axis in ('x', 'y', 'both')
if axis in ('x', 'both'):
vals, labels = process_ticks(ax.get_xticks())
if len(vals) > 0: ax.set_xticks(vals, labels)
if axis in ('y', 'both'):
vals, labels = process_ticks(ax.get_yticks())
if len(vals) > 0: ax.set_yticks(vals, labels)
def process_ticks(ticks):
vals = []
labels = []
for tick in ticks:
frac = Fraction(tick/pi)
if frac.numerator < 10 and frac.numerator < 10:
if frac.numerator == 0: label = '0'
elif frac.denominator == 1:
if frac.numerator == 1: label = '$\pi$'
elif frac.numerator == -1: label = '-$\pi$'
else: label = f'{frac.numerator} $\pi$'
elif frac.numerator == -1: label = f'-$\pi$/{frac.denominator}'
elif frac.numerator == 1: label = f'$\pi$/{frac.denominator}'
else: label = f'{frac.numerator}$\pi$/{frac.denominator}'
vals.append(tick)
labels.append(label)
return vals, labels
# Generate data
w_fr = np.linspace(-0.5*pi, 3.1*pi, 60)
H_func = lambda h, w: np.sum(h * np.exp(-1j * w[:, None] * np.arange(len(h))), axis=1)
r_fr = H_func([1, -1], w_fr)
# Prepare figure
fig, ax = plt.subplots(figsize=(10, 4), layout='constrained')
ax.grid()
ax.set_title('Frequency response')
ax.set_xlabel('normalized radian frequency')
ax.xaxis.set_major_locator(tck.MultipleLocator(base=pi/2))
g_c, p_c = 'C0', 'C1'
# Plot gain
ax.set_ylabel('amplitude', c=g_c)
ax.plot(w_fr, abs(r_fr), label='gain', c=g_c)
ax.tick_params(axis='y', labelcolor=g_c)
# Plot phase shift
ax1 = ax.twinx()
ax1.set_ylabel('phase shift', c=p_c)
ax1.yaxis.set_major_locator(tck.MultipleLocator(base=pi/4))
ax1.plot(w_fr, np.unwrap(np.angle(r_fr), period=2*pi), label='phase shift', c=p_c)
ax1.tick_params(axis='y', labelcolor=p_c)
# Convert floats to pi fractions
convert_to_pi_fractions(ax)
convert_to_pi_fractions(ax1, axis='y')
I cannot add a colorbar to my 3D scatter plot that is coloured in range of min and max according to the value of bifurWidth. I've tried various attempts shown on stackoverflow, none have had any success. Any help would really be appreciated, as I am at a major loss with this.
My most recent attempt is hashed out of the code shown below.
My code:
from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = get_cmap("jet")
fig.tight_layout()
fig.set_size_inches(25,15)
min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size
for idx, p in enumerate(dataSorted[:,1]):
powerLoop = dataSorted[idx,0]
powerLoop = powerLoop.astype(np.float)
bifurWidthLoop = dataSorted[idx,2]
bifurWidthLoop = bifurWidthLoop.astype(np.float)
y0 = genfromtxt(p, unpack=True, usecols=[0], skiprows=19, skip_footer=1)
length = len(x0)
power_array = [powerLoop] * length
bifurWidth_array = [bifurWidthLoop] * length
label = str(bifurWidth)
a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)
#cax = ax.imshow(y0, interpolation='nearest', vmin=min, vmax=max)
#fig.colorbar(cax)
fig.savefig('test.png',dpi=300)
Example of an attempt and its error:
If I use fig.colorbar(a) inside or outside of the plotting for loop, I return NoneType oject has no attribute autoscale_None.
Your code doesn't run (x0,dataSorted,y0,etc missing) so can't get it to work (also note x0,power_array,y0 are wrong order in fn call). You need to return the handle to the scatter plot in order to plot a colorbar. If you change your myScatter function to return the handle,
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
and then call plt.colorbar(a). A minimal(ish) example would be,
from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def myScatter(x0,y0,power_array,c,lw,s,vmin,vmax,cmap,label,ax):
return ax.scatter(x0,y0,power_array,c=c,lw=lw,s=s,vmin=min,vmax=max,cmap=cmhot,label=label)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = get_cmap("jet")
fig.tight_layout()
fig.set_size_inches(25,15)
min = 3 #colorbar range
max = 10
lw = 0 #linewidth
s = 10 #scatter size
label = 'test'
power_array = np.random.random((100,10))
bifurWidth_array = np.random.random((100,10))*(max-min)+min
x0 = np.random.random((100,10))
y0 = np.random.random((100,10))
a = myScatter(x0,power_array,y0,bifurWidth_array,lw,s,min,max,cmhot,label,ax)
plt.colorbar(a)
plt.show()
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!
hard code them in fractions or accept floating numbers
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(-10.0,10.0,0.1)
y=np.arctan(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'b.')
y_pi = y/np.pi
unit = 0.25
y_tick = np.arange(-0.5, 0.5+unit, unit)
y_label = [r"$-\frac{\pi}{2}$", r"$-\frac{\pi}{4}$", r"$0$", r"$+\frac{\pi}{4}$", r"$+\frac{\pi}{2}$"]
ax.set_yticks(y_tick*np.pi)
ax.set_yticklabels(y_label, fontsize=20)
y_label2 = [r"$" + format(r, ".2g")+ r"\pi$" for r in y_tick]
ax2 = ax.twinx()
ax2.set_yticks(y_tick*np.pi)
ax2.set_yticklabels(y_label2, fontsize=20)
plt.show()
the result is
i wrote a little function, that gives you back a list of labels:
import numpy as np
from fractions import Fraction
def create_pi_labels(a, b, step):
max_denominator = int(1/step)
# i added this line and the .limit_denominator to solve an
# issue with floating point precision
# because of floataing point precision Fraction(1/3) would be
# Fraction(6004799503160661, 18014398509481984)
values = np.arange(a, b+step/10, step)
fracs = [Fraction(x).limit_denominator(max_denominator) for x in values]
ticks = values*np.pi
labels = []
for frac in fracs:
if frac.numerator==0:
labels.append(r"$0$")
elif frac.numerator<0:
if frac.denominator==1 and abs(frac.numerator)==1:
labels.append(r"$-\pi$")
elif frac.denominator==1:
labels.append(r"$-{}\pi$".format(abs(frac.numerator)))
else:
labels.append(r"$-\frac{{{}}}{{{}}} \pi$".format(abs(frac.numerator), frac.denominator))
else:
if frac.denominator==1 and frac.numerator==1:
labels.append(r"$\pi$")
elif frac.denominator==1:
labels.append(r"${}\pi$".format(frac.numerator))
else:
labels.append(r"$\frac{{{}}}{{{}}} \pi$".format(frac.numerator, frac.denominator))
return ticks, labels
https://github.com/MaxNoe/python-plotting/blob/master/source/create_pi_labels.py
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
I created a PyPi Package that can format and place ticks at multiples of fractions of pi.
"""Show an example of using MultiplePi on the y-axis."""
import matplotlib.pyplot as plt
import numpy as np
from matplot_fmt_pi import MultiplePi
x = np.arange(-10.0, 10.0, 0.1)
y = np.arctan(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("MultiplePi formatting")
ax.plot(x, y, 'b.')
y_pi = y / np.pi
unit = 0.25
y_tick = np.arange(-0.5, 0.5 + unit, unit)
# New way
manager = MultiplePi(4)
ax.yaxis.set_major_locator(manager.locator())
ax.yaxis.set_major_formatter(manager.formatter())
# Other way
y_label2 = [r"$" + format(r, ".2g") + r"\pi$" for r in y_tick]
ax2 = ax.twinx()
ax2.set_yticks(y_tick * np.pi)
ax2.set_yticklabels(y_label2)
plt.savefig("./pi_y_axis.png", dpi=120)