我只想在饼图上的楔形之间绘制某些分界线。
在研究时,我发现我可以使用
wedgeprops
属性添加分隔线,或者在绘制图表后添加分隔线,如下所示:
wedges = plt.pie(sizes, labels=labels, colors=colours, autopct='%1.1f%%')
for wedge in wedges[0]:
wedge.set_lw(0.5)
wedge.set_edgecolor("black")
但是,据我所知,楔形的线宽属性适用于楔形周围的整个方向,并且我无法修改单个边缘。
我发现通过使用补丁,您可以为图表绘制一个轮廓圆,如下所示:
circle = matplotlib.patches.Circle(center, radius, fill=False, edgecolor="k", linewidth=2)
ax.add_patch(circle)
但是查看wiki,我看不到一行的补丁。我确实看到了
matplotlib.lines.Line2D
,但我不确定如何在这种情况下正确使用它。
这是我的完整代码。我试图不在“分配”(橙色)和“填充”)(鲑鱼)部分之间划出分界线。
def graph_memory_layout(memory_layout):
# Format: F(ree)/A(llocated)[number of bytes]P(adding)[number of bytes] [space] [next entry]
# Extract infomation
layout = memory_layout.split()
colours = []
sizes=[]
for memory in layout:
first_char = memory[0]
if (first_char == 'A'):
allocated_bytes = 0
padded_bytes = 0
padded_flag = None
for i, char in enumerate(memory[1:]):
if char == 'P':
padded_flag = i + 1
allocated_bytes = int(memory[1:padded_flag])
padded_bytes = int(memory[padded_flag + 1:])
payload_bytes = allocated_bytes - padded_bytes
sizes.append(payload_bytes)
colours.append(ALLOCATED_COLOUR)
if padded_bytes is not 0:
sizes.append(padded_bytes)
colours.append(PADDED_COLOUR)
# Perhaps I could track that these wedges shouldn't have a dividing line and use that infomation later somehow?
elif (first_char == 'F'):
sizes.append(memory[1:])
colours.append(FREE_COLOUR)
# Create pie chart
wedges = plt.pie(sizes, colors=colours)
for wedge in wedges[0]:
wedge.set_lw(0.5)
wedge.set_edgecolor("black")
plt.axis("equal")
plt.title("Memory Layout")
# Draw the chart
plt.show()
你走在正确的道路上。您需要勾勒出下部扇区的轮廓,并以饼图的半径画一个轮廓圆。
import matplotlib.pyplot as plt
import matplotlib.patches
if __name__ == '__main__':
data = [25.0, 25.0, 50.0]
fig, ax = plt.subplots()
wedges, _ = ax.pie(data)
wedges[-1].set_linewidth(0.5)
wedges[-1].set_edgecolor('k')
center = wedges[0].center
r = wedges[0].r
circle = matplotlib.patches.Circle(center, r, fill=False, edgecolor="k", linewidth=0.5)
ax.add_patch(circle)
plt.show()