简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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