Matplotlib подставляет пользовательские переменные ввода в Python - PullRequest
0 голосов
/ 12 октября 2018

Я пытаюсь написать программу на 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.Используя этот формат, я могу сделать то же самое для других квадрантов.Приложено окончательное изображение того, что я делаю вручную.Я хотел бы иметь возможность производить эти цвета автоматически. колесо квадрата цвета пончика

1 Ответ

0 голосов
/ 19 октября 2018

Вам нужно поместить цвета в один массив.Вы создали несколько массивов с одним значением для каждого.

Сначала вам нужно сохранить пользовательский ввод для всех квадрантов в одном массиве.Поэтому вы можете использовать следующую функцию:

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...