简体   繁体   中英

Plotting line graphs in matplotlib

I'm trying to plot a connected line graph. If I make changes to the code below, the plot fails to display.

My plot, as of now, is filled with unconnected dots.

How to do generate a line graph of connected points?

x = np.zeros(sheet_1.max_row)
y = np.zeros(sheet_1.max_row)

print (sheet_1.max_row)
print (sheet_1.max_column)

f = open("Bad_Data_Points_CD25.txt", "w")

for i in range(0, 10): #change to 1000
    for j in range(0, 289): # change to 289

        x[i] = sheet_1.cell(row=i + 1, column=j + 1).value #J + 1 changed to J
        print x[i]
        plt.plot(i, x[i],'go-', label='Values')



plt.grid(True)

plt.title("ABCD")
plt.ylabel("ABCD")
plt.ylim(0,0.15)
plt.xlabel("ABCD")
plt.xlim(0, 10)

plt.show()

Your loop structure is such that you are issuing one plot() call per data point. But you will only see connected lines if you plot a whole sequence at once.

I have made 3 changes below:

  • I have swapped the nesting order of the two loops because of the way you've chosen to overwrite and re-use x for each different value of j

  • I have unindented the plot command so that it's part of the j loop but not the i loop

  • I have changed the plot arguments to plot the whole of x at once

     for j in range(0, 289): # change to 289 for i in range(0, 10): x[i] = sheet_1.cell(row=i + 1, column=j + 1).value #J + 1 changed to J print x[i] plt.plot(x, 'go-', label='Values from column %d' % (j+1)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM