Hide radial tick labels matplotlib - python

I'm trying to plot a polar plot with this code:
import numpy as np
import matplotlib.pylab as plt
def power(angle, l, lam):
return 1/(lam) * ((np.cos(np.pi*l*np.cos(angle)/lam) - np.cos(np.pi*l/lam))/np.sin(angle))**2
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='polar')
theta = np.linspace(0.001, 2*np.pi, 100)
P1 = power(theta, 1, 5)
ax.plot(theta, P1, color='r', linewidth=3)
plt.savefig('1.png')
and I get this plot:
I would like to change 2 things. The first and more important one is to hide the radial tick labels (I just want to show the general form of the plot).
If possible, how can I choose the vertical axis to correspond to 0°?
Thanks for your help.

You can use set_yticklabels() to remove the radial ticks and set_theta_zero_location() to change the zero location:
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='polar')
ax.plot(theta, P1, color='r', linewidth=3)
ax.set_yticklabels([])
ax.set_theta_zero_location('N')
plt.show()
You might also want to change the direction of the azimuthal axis:
ax.set_theta_direction(-1)

You can set the theta zero position with ax.set_theta_zero_location('N').
To modify the r tick labels, you could do something like
for r_label in ax.get_yticklabels():
r_label.set_text('')
If you want to remove them entirely, do ax.set_yticklabels([]).
More methods can be found in the PolarAxes documentation.

Related

How to specify coordinates of a point in a matplotlib plot?

How can I make that when I plot a function (based on a np.array) certain values have their coordinates in the plot?
I know how to change color and other little things with code lines like:
line1, = plt.plot(t, f, '*-', label='force', color='#4F81BD') # blue
line2, = plt.plot(t, a, 'o-', label='acceleration', color='#C0504D') # red
but for example if I have a "peak" in the plot line, I don't know how to make their coordinates to appear in the same plot
This code snippet might help you:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)
ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]
#Labeling the graph (ymax+1 is defining the distance from the word to the point)
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))
ax.set_ylim(0,20)
plt.show()
Output:
I hope I could help you out a bit.

Matplotlib scatter plot of unfilled squares

I would like to make a scatter plot with unfilled squares. markerfacecolor is not an option recognized by scatter. I made a MarkerStyle but the fill style seems to be ignored by the scatter plot. Is there a way to make unfilled markers in the scatterplot?
import matplotlib.markers as markers
import matplotlib.pyplot as plt
import numpy as np
def main():
size = [595, 842] # in pixels
dpi = 72. # dots per inch
figsize = [i / dpi for i in size]
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0,0,1,1])
x_max = 52
y_max = 90
ax.set_xlim([0, x_max+1])
ax.set_ylim([0, y_max + 1])
x = np.arange(1, x_max+1)
y = [np.arange(1, y_max+1) for i in range(x_max)]
marker = markers.MarkerStyle(marker='s', fillstyle='none')
for temp in zip(*y):
plt.scatter(x, temp, color='green', marker=marker)
plt.show()
main()
It would appear that if you want to use plt.scatter() then you have to use facecolors = 'none' instead of setting fillstyle = 'none' in construction of the MarkerStyle, e.g.
marker = markers.MarkerStyle(marker='s')
for temp in zip(*y):
plt.scatter(x, temp, color='green', marker=marker, facecolors='none')
plt.show()
or, use plt.plot() with fillstyle = 'none' and linestyle = 'none' but since the marker keyword in plt.plot does not support MarkerStyle objects you have to specify the style inline, i.e.
for temp in zip(*y):
plt.plot(x, temp, color='green', marker='s', fillstyle='none')
plt.show()
either of which will give you something that looks like this
Refer to: How to do a scatter plot with empty circles in Python?
Try adding facecolors='none' to your plt.scatter
plt.scatter(x, temp, color='green', marker=marker, facecolors='none')

How to decide which bars are plotted on top/last in overlay of 2 Pandas bar plots where one plot uses alpha [duplicate]

In pyplot, you can change the order of different graphs using the zorder option or by changing the order of the plot() commands. However, when you add an alternative axis via ax2 = twinx(), the new axis will always overlay the old axis (as described in the documentation).
Is it possible to change the order of the axis to move the alternative (twinned) y-axis to background?
In the example below, I would like to display the blue line on top of the histogram:
import numpy as np
import matplotlib.pyplot as plt
import random
# Data
x = np.arange(-3.0, 3.01, 0.1)
y = np.power(x,2)
y2 = 1/np.sqrt(2*np.pi) * np.exp(-y/2)
data = [random.gauss(0.0, 1.0) for i in range(1000)]
# Plot figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax2.hist(data, bins=40, normed=True, color='g',zorder=0)
ax2.plot(x, y2, color='r', linewidth=2, zorder=2)
ax1.plot(x, y, color='b', linewidth=2, zorder=5)
ax1.set_ylabel("Parabola")
ax2.set_ylabel("Normal distribution")
ax1.yaxis.label.set_color('b')
ax2.yaxis.label.set_color('r')
plt.show()
Edit: For some reason, I am unable to upload the image generated by this code. I will try again later.
You can set the zorder of an axes, ax.set_zorder(). One would then need to remove the background of that axes, such that the axes below is still visible.
ax2 = ax1.twinx()
ax1.set_zorder(10)
ax1.patch.set_visible(False)

PyPlot move alternative y axis to background

In pyplot, you can change the order of different graphs using the zorder option or by changing the order of the plot() commands. However, when you add an alternative axis via ax2 = twinx(), the new axis will always overlay the old axis (as described in the documentation).
Is it possible to change the order of the axis to move the alternative (twinned) y-axis to background?
In the example below, I would like to display the blue line on top of the histogram:
import numpy as np
import matplotlib.pyplot as plt
import random
# Data
x = np.arange(-3.0, 3.01, 0.1)
y = np.power(x,2)
y2 = 1/np.sqrt(2*np.pi) * np.exp(-y/2)
data = [random.gauss(0.0, 1.0) for i in range(1000)]
# Plot figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax2.hist(data, bins=40, normed=True, color='g',zorder=0)
ax2.plot(x, y2, color='r', linewidth=2, zorder=2)
ax1.plot(x, y, color='b', linewidth=2, zorder=5)
ax1.set_ylabel("Parabola")
ax2.set_ylabel("Normal distribution")
ax1.yaxis.label.set_color('b')
ax2.yaxis.label.set_color('r')
plt.show()
Edit: For some reason, I am unable to upload the image generated by this code. I will try again later.
You can set the zorder of an axes, ax.set_zorder(). One would then need to remove the background of that axes, such that the axes below is still visible.
ax2 = ax1.twinx()
ax1.set_zorder(10)
ax1.patch.set_visible(False)

Python - Add residuals to subplots generated by a for loop

I just can't get the subplots to work when adding the residuals with add_axes. It works well without residuals and I can add residuals to just one plot. This is an example of what I'm doing:
First, just to give you an idea of what I'm ploting, (t, y) is the data I want to plot, fit is the fit to the data, and diff is the difference between fit and data.
t, s, fit = [], [], []
diff = []
for i in range(12):
t.append(x / y[i])
s.append(np.linspace(0, 1, num=100, endpoint=True))
fit.append(UnivariateSpline(t[i], y, er, s=5e20))
diff.append(fit[i](t[i]) - y)
And this is the figure:
fig = plt.figure()
for i in range(12):
plt.subplot(4,3,i+1)
fig.add_axes((0.,0.3,0.7,0.9))
plt.plot(s[i], fit[i](s[i]), 'r-') # this is the fit
plt.errorbar(t[i], y, er, fmt='.k',ms=6) # this is the data
plt.axis([0,1, 190, 360])
fig.add_axes((0.,0.,0.7,0.3))
plot(t[i],diff[i],'or') # this are the residuals
plt.axis([0,1, 190, 360])
So as you can see I'm generating 12 subplots, which works just fine until I add the fig.add_axes to separate each subplot between data+fit and the residuals, but what I get is one messy plot on top of the subplots (figure has been shrunken to see the subplots under):
And what I want is 12 subplots where each one looks like this:
Usually plt.subplot(..) and fig.add_axes(..) are complementary. This means that both commands create an axes inside the figure.
Their usage however would be a bit different. To create 12 subplots with subplot you would do
for i in range(12):
plt.subplot(4,3,i+1)
plt.plot(x[i],y[i])
To create 12 subplots with add_axes you would need to do something like this
for i in range(12):
ax = fig.add_axes([.1+(i%3)*0.8/3, 0.7-(i//3)*0.8/4, 0.2,.18])
ax.plot(x[i],y[i])
where the positions of the axes need to be passed to add_axes.
Both work fine. But combining them is not straight forward, as the subplots are positionned according to a grid, while using add_axes you would need to already know the grid positions.
So I would suggest starting from scratch. A reasonable and clean approach to create subplots is to use plt.subplots().
fig, axes = plt.subplots(nrows=4, ncols=3)
for i, ax in enumerate(axes.flatten()):
ax.plot(x[i],y[i])
Each subplot can be divided into 2 by using an axes divider (make_axes_locatable)
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(ax)
ax2 = divider.append_axes("bottom", size=size, pad=pad)
ax.figure.add_axes(ax2)
So looping over the axes and doing the above for every axes allows to get the desired grid.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.rcParams["font.size"] = 8
x = np.linspace(0,2*np.pi)
amp = lambda x, phase: np.sin(x-phase)
p = lambda x, m, n: m+x**(n)
fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(8,6), sharey=True, sharex=True)
def createplot(ax, x, m, n, size="20%", pad=0):
divider = make_axes_locatable(ax)
ax2 = divider.append_axes("bottom", size=size, pad=pad)
ax.figure.add_axes(ax2)
ax.plot(x, amp(x, p(x,m,n)))
ax2.plot(x, p(x,m,n), color="crimson")
ax.set_xticks([])
for i in range(axes.shape[0]):
for j in range(axes.shape[1]):
phase = i*np.pi/2
createplot(axes[i,j], x, i*np.pi/2, j/2.,size="36%")
plt.tight_layout()
plt.show()

Categories

Resources