如何在 matplotlib 中叠加图形?

发布于 2025-01-18 19:54:37 字数 1806 浏览 1 评论 0原文

我使用条形图创建了环和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 scatterfunction, the plotted dots do not overlay with the bar graphs. This only works with the plotfunction.

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:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-01-25 19:54:37

只需使用参数 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()

在此处输入图像描述

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文