简体   繁体   English

Matplotlib替代Python中的用户输入变量

[英]Matplotlib Substitute User Input Variables in Python

I am trying to write a program in python that will accept user input to set the colors of a donut pie chart in matplotlib. 我正在尝试用python编写一个程序,该程序将接受用户输入以在matplotlib中设置甜甜圈饼图的颜色。 Here is what I have that is currently working: 这是我目前正在工作的东西:

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

Here is what I want to happen: 这是我想发生的事情:

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

I don't know how to call the variable in to the colors property for ax.pie. 我不知道如何将变量调用到ax.pie的colors属性中。 Using this format, I can do the same for the other quadrants. 使用这种格式,我可以对其他象限执行相同的操作。 Attached is the final picture of what I'm producing manually. 随附的是我手动制作的最终照片。 I'd like to be able to produce these colors automatically. 我希望能够自动产生这些颜色。 donut color quadrant wheel 甜甜圈色象限轮

You need to put the colors into a single array. 您需要将颜色放入单个阵列中。 You generated multiple arrays with one value for each. 您生成了多个数组,每个数组都有一个值。

First you need to save the user input for all quadrants into a single array. 首先,您需要将所有象限的用户输入保存到单个数组中。 Therefore you can use following function: 因此,您可以使用以下功能:

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)

You can now use this function like 您现在可以使用此功能,例如

colorList = []
quadrants = ["First", "Second", "Third", "Fourth"]
for quadrant in quadrants:
    getQuadrantColor(colorList, quadrant)

Now you got all color information and can create the pie chart 现在您获得了所有颜色信息,可以创建饼图

mypie4, _ =ax.pie(factor_size, radius=5-1.4, colors=colorList)
plt.setp(mypie4, width=.2, edgecolor='black')
plt.margins(0,0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM