Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

python - How to plot the piecewise continuous points in matplotlib

My dataset is as follows:

G R Y
1 1 0
1 2 1
1 3 2
1 4 4
1 5 2
2 1 1
2 2 2
2 3 3
2 4 2
3 1 0
3 2 1
3 3 2
3 4 2
3 5 3

I want to know how to write the correct matplotlib code to plot the points as follows:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could just split up your data according to the three sections you have, graph each group separately, and then attach the graphs together:

fig, axes = plt.subplots(1, 3, sharey=True)
Y = [0, 1, 2, 4, 2, 1, 2, 3, 2, 0, 1, 2, 2, 3]
Y0 = Y[0:6]
Y1 = Y[5:10]
Y2 = Y[9:15]
axes[0].plot(Y0)
axes[1].plot(Y1)
axes[2].plot(Y2)
plt.ylim([0, 5])
subplots_adjust(wspace=0)

That will get you pretty close to what you need (though I admit some of the the x-axes could use a little extra formatting):

enter image description here

If I were you I would enter that line by line, hitting plt.draw() after every line of matplotlib code to see what exactly is going on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...