我从同事那里得到了以下代码。它绘制了一个极坐标图,显示了三个环上三个事物的百分比。我不太清楚代码中极坐标图是如何绘制的,但它似乎是从条形图转换而来的。我使用的 matplotlib 版本是 3.9.2。我无法让环周围的方框消失。正如您在示例代码中看到的那样,我通过在线搜索尝试了各种方法。在 Matplotlib 3.7.0 上运行相同的代码没有显示方框。我觉得这可能是一个简单的单行修复,但无法弄清楚。希望有人能在这里帮助我。谢谢。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from math import pi
data = [15, 20, 12]
fig, ax = plt.subplots(figsize=(6, 6), facecolor='white')
ax = plt.subplot(projection='polar', frameon=False)
startangle = 90
colors = ['#005a85', '#4c4c4d', '#048543']
colors_bg = ['#818589', '#818589', '#818589']
xs = [(i * pi *2)/ 100 for i in data]
xs_bg = [(i * pi *2)/ 100 for i in [100,100,100]]
ys = [-0.2, 1, 2.2]
left = (startangle * pi * 6)/ 360 #this is to control where the bar starts
# plot bars and points at the end to make them round
for i, x in enumerate(xs_bg):#draw the background grey circles
ax.barh(ys[i], x, left=left, height=0.4, color=colors_bg[i])
for i, x in enumerate(xs):
ax.barh(ys[i], x, left=left, height=0.4, color=colors[i])
ax.scatter(x+left, ys[i], s=60, color=colors[i], zorder=2)
ax.scatter(left, ys[i]-0.01, s=60, color=colors[i], zorder=2)
plt.ylim(-4, 4)
# clear ticks, grids, spines
plt.xticks([])
plt.yticks([])
ax.spines.clear()
# Other attempts to remove the frame, none worked
ax.axis("off")
ax.set_axis_off()
ax.grid(False)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
plt.tight_layout()
plt.show()
您创建了两个相互重叠的子图。一个带有
plt.subplots
,另一个带有plt.subplot
。在版本 3.7 中,当第二个子图添加到顶部时,原始子图会被自动删除。但这在 v3.8 中发生了变化。在使用 v3.7 运行时,您应该会看到弃用警告。您成功隐藏了第二个子图,但没有隐藏第一个子图。要修复此问题,只需不要创建第一个子图。例如,将此行替换只创建一个图形