【问题标题】:Plotting line graph绘制折线图
【发布时间】:2016-07-31 20:52:19
【问题描述】:

在尝试绘制折线图时,我收到以下错误消息:

Traceback(最近一次调用最后一次): 文件“U:/My Documents/Python/Project1_real.py”,第 119 行,在 主要的() 文件“U:/My Documents/Python/Project1_real.py”,第 40 行,在 main line_graph(return_lines) 文件“U:/My Documents/Python/Project1_real.py”,第 100 行,在 line_graph plt.plot(range(len(myGraphValues)), myGraphValues, 标记, 线, 颜色) 文件“C:\Python34\lib\site-packages\matplotlib\pyplot.py”,第 3154 行,在图中 ret = ax.plot(*args, **kwargs) 文件“C:\Python34\lib\site-packages\matplotlib__init__.py”,第 1811 行,在内部 返回函数(ax,*args,**kwargs) 文件“C:\Python34\lib\site-packages\matplotlib\axes_axes.py”,第 1427 行,在图中 对于 self._get_lines(*args, **kwargs) 中的行: _grab_next_args 中的文件“C:\Python34\lib\site-packages\matplotlib\axes_base.py”,第 395 行 对于self._plot_args中的seg(剩余[:isplit],kwargs): _plot_args 中的文件“C:\Python34\lib\site-packages\matplotlib\axes_base.py”,第 364 行 x, y = self._xy_from_xy(x, y) _xy_from_xy 中的文件“C:\Python34\lib\site-packages\matplotlib\axes_base.py”,第 223 行 raise ValueError("x 和 y 必须具有相同的第一维") ValueError: x 和 y 必须具有相同的第一维

这是我的代码:

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

【问题讨论】:

    标签: python


    【解决方案1】:

    您没有相同数量的 X 和 Y 值传递给绘图函数。

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 1970-01-01
      • 2019-10-27
      • 2016-04-06
      • 1970-01-01
      • 2018-01-26
      • 2017-03-22
      相关资源
      最近更新 更多