如何在 matplotlib 中叠加图形?
我使用条形图创建了环和Matplotlib的极性投影。
现在,我想将单个数据点绘制到这些环上。 使用plt.plot
此外,我想根据源数据的不同列的类别对数据点进行颜色编码。使用例如ax.scatter(df [xcol],df [ycol],c = df.color)
,
但是,如果我使用scatse> stacter
函数, 则可以正常工作。绘制的点不与条形图叠加。这仅适用于绘图
函数。
有人知道我如何解决这个问题吗?
这是我当前的代码:
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用参数
zorder
Just use the argument
zorder