在尝试绘制折线图时,我收到以下错误消息:
追踪(最近一次通话): File" U:/ My Documents / Python / Project1_real.py",第119行,in 主要() 文件" U:/我的文档/ Python / Project1_real.py",第40行,在main中 line_graph(return_lines) 文件" U:/我的文档/ Python / Project1_real.py",第100行,在line_graph中 plt.plot(range(len(myGraphValues)),myGraphValues,marker,line,color) 文件" C:\ Python34 \ lib \ site-packages \ matplotlib \ pyplot.py",第3154行,在图中 ret = ax.plot(* args,** kwargs) 文件" C:\ Python34 \ lib \ site-packages \ matplotlib__init __。py",1811行,内在 return func(ax,* args,** kwargs) 文件" C:\ Python34 \ lib \ site-packages \ matplotlib \ axes_axes.py",第1427行,在图中 对于self._get_lines中的行(* args,** kwargs): 文件" C:\ Python34 \ lib \ site-packages \ matplotlib \ axes_base.py",第395行,在_grab_next_args中 对于self._plot_args中的seg(剩余[:isplit],kwargs): 文件" C:\ Python34 \ lib \ site-packages \ matplotlib \ axes_base.py",第364行,在_plot_args中 x,y = self._xy_from_xy(x,y) 文件" C:\ Python34 \ lib \ site-packages \ matplotlib \ axes_base.py",第223行,在_xy_from_xy中 提高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