I have created rings using bar graphs and the polar projection of matplotlib.
Now I would like to plot single data points onto these rings.
This works fine using plt.plot
Furthermore, I would like to color code the data points based on categories of a different column of the source data. This would work fine using for example ax.scatter(df[xcol], df[ycol], c=df.Color)
However, if I use the scatter
function, the plotted dots do not overlay with the bar graphs. This only works with the plot
function.
Does anyone know how I could solve this problem?
This is my current code:
import numpy as np
import matplotlib.pyplot as plt
# draw 4 rings
#Create plot with polar projection for circle structure
fig, ax = plt.subplots(figsize=(12, 8),
subplot_kw=dict(projection='polar'))
# remove axes from plot
plt.axis('off')
# create ring coordinates
theta = np.linspace(0, 2 * np.pi, 768)
r = np.linspace(4, 16, 4)
r_ring1 = r[3]-(r[3]-r[2])/2
r_ring2 = r[2]-(r[2]-r[1])/2
r_ring3 = r[1]-(r[1]-r[0])/2
r_ring4 = r[0]/2
# plot the 4 rings
for i in range(r.shape[0]):
ax.plot(theta, np.repeat(r[i], theta.shape), '-k', lw=1)
#fill rings with radial bar plots
ax.barh(r_ring4, np.pi *2, height=r[0], color='#ff46a6')
ax.barh(r_ring3, np.pi *2, height=r_ring2-r_ring3, color='#aaeb8f')
ax.barh(r_ring2, np.pi *2, height=r_ring2-r_ring3, color='#a2c4c9')
ax.barh(r_ring1, np.pi *2, height=r_ring1-r_ring2, color='#5DADE2')
#plotting single data points with plot works
for i in range(5):
plt.plot(0.15*np.pi i, r_ring1-0.2, marker='o', markersize=7.0, markeredgewidth=1.5,
markerfacecolor='red', markeredgecolor='white')
# plotting with scatter is only visible if ax.barh(r_ring2,...) is removed
for i in range(5):
ax.scatter(0.15*np.pi i, r_ring2-0.2, c='red' )
plt.show()
Resulting in the following image:
CodePudding user response:
Just use the argument zorder
import matplotlib.pyplot as plt
# draw 4 rings
#Create plot with polar projection for circle structure
fig, ax = plt.subplots(figsize=(12, 8),
subplot_kw=dict(projection='polar'))
# remove axes from plot
plt.axis('off')
# create ring coordinates
theta = np.linspace(0, 2 * np.pi, 768)
r = np.linspace(4, 16, 4)
r_ring1 = r[3]-(r[3]-r[2])/2
r_ring2 = r[2]-(r[2]-r[1])/2
r_ring3 = r[1]-(r[1]-r[0])/2
r_ring4 = r[0]/2
# plot the 4 rings
for i in range(r.shape[0]):
ax.plot(theta, np.repeat(r[i], theta.shape), '-k', lw=1)
#fill rings with radial bar plots
ax.barh(r_ring4, np.pi *2, height=r[0], color='#ff46a6', zorder=0)
ax.barh(r_ring3, np.pi *2, height=r_ring2-r_ring3, color='#aaeb8f', zorder=0)
ax.barh(r_ring2, np.pi *2, height=r_ring2-r_ring3, color='#a2c4c9', zorder=0)
ax.barh(r_ring1, np.pi *2, height=r_ring1-r_ring2, color='#5DADE2', zorder=0)
#plotting single data points with plot works
for i in range(5):
plt.plot(0.15*np.pi i, r_ring1-0.2, marker='o', markersize=7.0, markeredgewidth=1.5,
markerfacecolor='red', markeredgecolor='white')
# plotting with scatter is only visible if ax.barh(r_ring2,...) is removed
for i in range(5):
ax.scatter(0.15*np.pi i, r_ring2-0.2, c='black', zorder=1)
plt.show()