I have the following code that draws two superimposed plots from a two-dimensional list using matplotlib:
for day in days:
# Draw a green triangle
plt.plot(day[0], len(day[1] * 100), 'g^')
# Draw red dots
for hour in day[1]:
plt.plot(day[0], hour, 'ro')
This results in a plot like the following:
But I'd like the triangles to be connected with solid lines so that the evolution along the X-axis is seen more clearly. No matter what I replace 'g^' with I can't seem to draw solid lines here. How is this supposed to be done?
Thank you.
Welcome to Stack Overflow!
If you really want to loop through the data and plot it one-by-one you could do the following:
green_triangles,_ = plt.plot([],[],'g^')#Could be that you need 'g^-'. Can't test it now
for day in days:
# Draw a green triangle
X = np.append(green_triangles.get_xdata(), day[0])
Y = np.append(green_triangles.get_ydata(), len(day[1] * 100))
green_triangles.set_xdata(X)
green_triangles.set_ydata(Y)
# Draw red dots
for hour in day[1]:
plt.plot(day[0], hour, 'ro')
You can just do two lines to make the triangles connected, it is not beautiful, but works:
# Draw a green triangles, as you said:
plt.plot(day[0], len(day[1] * 100), 'g^')
# another line to draw the green line:
plt.plot(day[0], len(day[1] * 100), 'g')
Or, after reading matplotlib documentation , you can write:
plt.plot(day[0], len(day[1] * 100), linestyle='solid', marker='^', color='g')
Just replace
plt.plot(day[0], len(day[1] * 100), 'g^')
with
plt.plot(day[0], len(day[1] * 100), '-g^')
Notice the additional -
in the second line.
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.