如何使用matplotlib创建饼图圆形设计?

cvxl0en2  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(112)

Here is the problem,How to create this designHere is an example的数据库
尝试尝试改变颜色,半径仍然没有得到正确的设计,并得到的设计,我尝试了各种各样的事情,以使它完美,但它仍然总是得到一个错误的设计结束

plt.figure(figsize=(6,4))
 
colors = ['r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w','r','w']
labels = np.ones(20)         
#labels = [1.0,1.0,1.0,1.0,1.0,.........,1.0]
plt.pie([1], colors="k", radius = 2.05)
plt.pie(labels, colors=colors, radius = 2.0)

 
plt.pie([1],colors="g", radius = 1.8)
plt.pie([1], colors="y", radius = 1.6)
plt.pie([1], colors="c", radius = 1.3)
plt.pie([1], colors="b", radius = 1.1)
plt.pie([1], colors="m", radius = 0.9)
plt.pie([1], colors="b", radius = 0.3)

plt.pie(labels, colors=colors, radius = 0.25)
 
plt.pie([1], colors="w", radius = 0.2)
plt.pie([1], colors="k", radius = 0.1)
 
plt.show()

字符串

bksxznpy

bksxznpy1#

当你创建一个饼图时,你可以传递一个colors参数,它将循环使用。如果你给予它["w", "k"],它将交替白色和黑色,如果你给它["k", "w"],它将交替黑色和白色。因此,我们可以通过不同的半径循环,并不断反转颜色以创建您想要的图案。
我已经放了很多注解来向你解释代码,但是你应该意识到我展示的是比普通程序中更多的注解。实际上,注解只应在必要时用于解释代码。如果有人熟悉一个库,比如matplotlib和numpy,我不会像下面的代码那样解释基本函数。

import matplotlib.pyplot as plt
import numpy as np

# creates the figure
plt.figure()

# creates an all black pie chart (this is used for the black border)
plt.pie([1], colors="k", radius = 2.05)

# colors for the pie slices
colors = ["w", "k"]
stripes = 20    # number of stripes/slices

# data to be plotted; they're all 1s, so it will make them all equally-sized
# slices; makes as many as the stripes variable
data = np.ones(stripes)

# np.arange creates a list of values from the first argument to the second 
# argument with a step size of the last argument
for radius in np.arange(2, 0.01, -0.1):
    plt.pie(data, colors=colors, radius=radius)
    colors = colors[::-1]       # reverse the colors so the stripes alternate
 
plt.tight_layout()
plt.show()

字符串


的数据

相关问题