我使用條形圖和 matplotlib 的極坐標投影創建了環。
現在我想在這些環上繪制單個資料點。這很好用plt.plot
此外,我想根據源資料的不同列的類別對資料點進行顏色編碼。例如,這可以正常作業ax.scatter(df[xcol], df[ycol], c=df.Color)
但是,如果我使用該scatter
函式,繪制的點不會與條形圖重疊。這僅適用于該plot
功能。
有誰知道我該如何解決這個問題?
這是我當前的代碼:
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()
結果如下圖:
uj5u.com熱心網友回復:
只需使用引數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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457255.html
標籤:Python 麻木的 matplotlib 阴谋 极坐标
下一篇:從.txt檔案創建數字折線圖