0
votes

when trying to plot line graph I get this error message:

Traceback (most recent call last): File "U:/My Documents/Python/Project1_real.py", line 119, in main() File "U:/My Documents/Python/Project1_real.py", line 40, in main line_graph(return_lines) File "U:/My Documents/Python/Project1_real.py", line 100, in line_graph plt.plot(range(len(myGraphValues)), myGraphValues, marker, line, color) File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3154, in plot ret = ax.plot(*args, **kwargs) File "C:\Python34\lib\site-packages\matplotlib__init__.py", line 1811, in inner return func(ax, *args, **kwargs) File "C:\Python34\lib\site-packages\matplotlib\axes_axes.py", line 1427, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python34\lib\site-packages\matplotlib\axes_base.py", line 395, in _grab_next_args for seg in self._plot_args(remaining[:isplit], kwargs): File "C:\Python34\lib\site-packages\matplotlib\axes_base.py", line 364, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Python34\lib\site-packages\matplotlib\axes_base.py", line 223, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension

Here is my code:

import matplotlib.pyplot as plt

def main():
    valid = True
    another = "y"
    while another == "y" or another == "Y":

        while valid == True:
            plotGraph = input("What type of plot do you want, Line, Bar, Pie or Exit?")

            if plotGraph == "Line" or plotGraph == "line":
                print("Line")        
                valid == False
                graph_file = input("What is the name of the data file?")
                break
            elif plotGraph == "Bar" or plotGraph == "bar":
                print("Bar")
                valid == False
                graph_file = input("What is the name of the data file?")
                break
            elif plotGraph == "Pie" or plotGraph == "pie":
                print("Pie")
                valid == False
                graph_file = input("What is the name of the data file?")
                break
            elif plotGraph == "Exit" or plotGraph == "exit":
                print("Thanks for plotting")
                valid == False
                break
            else:
                print("Enter a valid response")

        return_lines = readData(graph_file)

        if plotGraph == "Line" or plotGraph == "line":
            line_graph(return_lines)

        elif plotGraph == "Bar" or plotGraph == "bar":
            bar_graph(return_lines)

        elif plotGraph == "Pie" or plotGraph == "pie":
            pie_graph(return_lines)

    print("Would you like to make another graph?")
    another = input("Y = yes, anything else = no:")

#this function takes one argument- graph_file, reads the file, 
#adds the data to a list and returns the info
#in the file through the variable return_lines
def readData(file_name):
    data_file = open(file_name, "r")
    file_contents = data_file.readline()

    #while file_contents != " ":
        #file_contents = data_file.readline()

    return file_contents 



#this function creates the line graph and accepts one argument-the return_lines variable 
#and constructs a line graph using the data in the list return_files
def line_graph(make_plot):
    myGraphValues = (make_plot)

    valid = True
    while valid == True:
        marker = input("Which marker would you like, type 'o' for circle, 's' for square," + \
                       " '*' for star, or 'D' for diamond?")

        if marker == "o" or marker == "s" or marker == "D" or marker == "*":
            print("Got it!")
            valid == False

        else:
            print("Enter a valid response")

        line = input("What line would you like, type '-' for solid, '--' for dashed, " + \
                     "or ':' for dotted?")
        if line == "-" or line == "--" or line == ":":
            print("Got it!")
            valid == False

        else:
            print("Enter a valid response")

        color = input("What color would you like it to be, type 'r' for red, " + \
                      "'g' for green or 'b' for blue?")
        if color == "r" or color == "g" or color == "b":
            print("Got it!")
            valid == False

        else:
            ("Enter a valid response")

        plt.plot(range(len(myGraphValues)), myGraphValues, marker, line, color)
        line_title = input("What is the title of the graph?")
        plt.title(line_title)

        ax = plt.axes()
        ax.set_xlim([0, max(myGraphValues) + 1])
        ax.set_ylim[(0, max(myGraphValues) + 10)]



    plt.show
1

1 Answers

0
votes

You don't have an equal number of X and Y values being passed to the plot function.