我正在尝试用python编写一个程序,该程序将接受用户输入以在matplotlib中设置甜甜圈饼图的颜色。 这是我目前正在使用的东西:
#3 ring 3 - Factors
mypie4, _ =ax.pie(factor_size, radius=5-1.4, colors=[a(0.85), a(0.85),
g(0.0), a(0.85),
g(0.0),
b(0.7), b(0.7), b(0.7),
c(0.85), c(0.85),
c(0.85), c(0.85),
g(0.0), g(0.0),
d(0.85), d(0.85),
g(0.0)])
plt.setp(mypie4, width=.2, edgecolor='black')
plt.margins(0,0)
这就是我想发生的事情:
## Show menu ##
print (30 * '-')
print (" Color Choices for First Quadrant")
print (30 * '-')
print ("1. Blue")
print ("2. Orange")
print ("3. Green")
print ("4. Purple")
print (30 * '-')
## Get input ###
choice = raw_input('Enter your choice [1-4] : ')
### Convert string to int type ##
choice = int(choice)
### Take action as per selected menu-option ###
if choice == 1:
user_color = [plt.cm.Blues(0.75)]
elif choice == 2:
user_color = [plt.cm.Oranges(0.75)]
elif choice == 3:
user_color = [plt.cm.Greens(0.75)]
elif choice == 4:
user_color = [plt.cm.Purples(0.75)]
else: ## default ##
print ("Invalid number. Try again...")
#3 ring 3 - Factors
mypie4, _ =ax.pie(factor_size, radius=5-1.4, colors=[[user_color],
[user_color],
[user_color],
[user_color],
[user_color],
b(0.7), b(0.7), b(0.7),
c(0.85), c(0.85),
c(0.85), c(0.85),
g(0.0), g(0.0),
d(0.85), d(0.85),
g(0.0)])
plt.setp(mypie4, width=.2, edgecolor='black')
plt.margins(0,0)
我不知道如何将变量调用到ax.pie的colors属性中。使用这种格式,我可以对其他象限执行相同的操作。随附的是我手动制作的最终照片。我希望能够自动产生这些颜色。donut color quadrant wheel
答案 0 :(得分:0)
您需要将颜色放入单个阵列中。您生成了多个数组,每个数组都有一个值。
首先,您需要将所有象限的用户输入保存到单个数组中。因此,您可以使用以下功能:
def getQuadrantColor(colorList, quadrantName):
print (30 * '-')
print (" Color Choices for {} Quadrant".format(quadrantName))
print (30 * '-')
print ("1. Blue")
print ("2. Orange")
print ("3. Green")
print ("4. Purple")
print (30 * '-')
## Get input ###
choice = raw_input('Enter your choice [1-4] : ')
### Convert string to int type ##
choice = int(choice)
### Take action as per selected menu-option ###
if choice == 1:
colorList.append(plt.cm.Blues(0.75))
elif choice == 2:
colorList.append(plt.cm.Oranges(0.75))
elif choice == 3:
colorList.append(plt.cm.Greens(0.75))
elif choice == 4:
colorList.append(plt.cm.Purples(0.75))
else: ## default ##
print ("Invalid number. Try again...")
getQuadrantColor(colorList, quadrantName)
您现在可以像使用此功能
colorList = []
quadrants = ["First", "Second", "Third", "Fourth"]
for quadrant in quadrants:
getQuadrantColor(colorList, quadrant)
现在您已经获得了所有颜色信息,并且可以创建饼图
mypie4, _ =ax.pie(factor_size, radius=5-1.4, colors=colorList)
plt.setp(mypie4, width=.2, edgecolor='black')
plt.margins(0,0)