问题设置matplot指向颜色和形状

时间:2016-07-08 20:36:07

标签: python python-3.x

每当我运行display_data函数(在成功回答至少一个calculation_game问题后有数据运行时),我会收到错误消息:

"ValueError: to_rgba: Invalid rgba arg "o"
to_rgb: Invalid rgb arg "o"
could not convert string to float: 'o'" 

当我在没有" o"的情况下运行代码时定义它工作正常的点的形状并显示蓝点。唯一的问题是我希望能够定义诸如" o"之类的形状。和" v"因为该图将在完成后显示来自多个列表的数据。有什么想法吗?

注意:

下面缺少功能。我在这里删除了它们,因为这个问题不需要它们。

import random
from random import randint
import time
import math
import matplotlib.pyplot as plt


# Number of problems for each practice/real round
practice_round = 0
real_round = 3

main_record = []
CALC_RECORD = []

# (1) Calculation Game ---------------------------------------------------------
'''Calculation game is a math game'''
def calculation():
    response_time = None
    # Determine the min and max calculation values
    min_calculation_value = 1
    max_calculation_value = 10
    # Generate the problems
    print('\nSolve the following problem:')
    a = random.randint(min_calculation_value, max_calculation_value)
    b = random.randint(min_calculation_value, max_calculation_value)
    problem_type = random.randint(1,2)
    if problem_type == 1:
        answer = a * b
        print(a, '*', b)
    elif problem_type == 2:
        answer = a % b
        print(a, '%', b)
    # Get the user's answer determine what to do if correct
    start_time = time.time()
    user_answer = input('\nEnter your answer: ')
    end_time = time.time()
    if user_answer == str(answer):
        response_time = end_time - start_time
        print('You are correct!')
    elif user_answer != str(answer):
        print('Oops, you are incorrect.')
    # Return game id, start time, and response time
    return("calculation", start_time, response_time)

def calculation_game():
    record = []
    # Generate two problems for a practice round
    print("\nLet's begin with 2 practice problems.")
    for i in range (practice_round):
        print('\nPractice Problem', i + 1, 'of', practice_round)
        calculation()
    # Generate 10 problems for a real, recorded round
    print("\nNow let's try it for real this time.")
    for i in range (real_round):
        print('\nProblem', i + 1, 'of', real_round)
        # Append records for each iteration
        record.append(calculation())
    main_record.extend(record)
    CALC_RECORD.extend(record)
    return record

# (5) Display Data -------------------------------------------------------------
def display_data():
    plt.ylabel('Time Per Question')
    plt.xlabel('Round Played')
    CALC_RECORD.sort(key=lambda record:record[1])
    calc_time = [t[2] for t in CALC_RECORD if t[0] =='calculation' and t[2] != None]
    alist=[i for i in range (len(calc_time))]
    if len(calc_time) >0:
        print (calc_time)
        x = alist
        y = calc_time
        plt.scatter(x, y, c="bo")
        plt.show(block=True)
    main_menu()


# (6) Save Progress ------------------------------------------------------------



# (7) Load Data ----------------------------------------------------------------



# (8) Quit Game ----------------------------------------------------------------

def quit_game():
    print('\nThank you for playing!')

# Main Menu --------------------------------------------------------------------

def menu():
    print("\nEnter 1 to play 'Calculation'")
    print("Enter 2 to play 'Binary Reader'")
    print("Enter 3 to play 'Trifacto'")
    print("Enter 4 to view your statistics")
    print("Enter 5 to display data")
    print("Enter 6 to save your progress")
    print("Enter 7 to load data")
    print("Enter 8 to quit the game")

def main_menu():

    print('Welcome!')
    user_input = ''
    while user_input != '8':
        menu()
        user_input = input('\nWhat would you like to do? ')
        if user_input == '1':
            calculation_game()
        if user_input == '2':
            binary_reader_game()
        if user_input == '3':
            trifacto_game()
        if user_input == '4':
            display_statistics()
        if user_input == '5':
            display_data()
        if user_input == '8':
            quit_game()

main_menu()

1 个答案:

答案 0 :(得分:0)

知道了,只需从plt.scatter(x, y, c="bo")

中删除c =部分