简体   繁体   中英

Create two value axis and plot line segments matplotlib

I am having a hard time trying to plot some values and creating line segments between the two data points with matplotlib(new with matplotlib). So far my code looks like this:

import matplotlib.pyplot as plt

datax1=[0,0,0,0,0]
datay1=[1,2,3,4,5]

datax2=[1,1,1,1,1]
datay2=[1,4,9,16,25] 

plt.plot(datax1, datay1)
plt.plot(datax2, datay2)

plot.show()

But so far I have been unsuccessful in creating a graph that I want after tweaking. I was wondering if it would be possible to produce a graph something like this in matplotlib:

在此处输入图片说明

Sort of like a "before and after" kind of a graph. So you would draw a line segment from (0,5) which is the "before" point to (1,25) which is the "after" point. I tried using plt.scatterplot and it plots the points but I wasnt sure how to fix the axis and draw a line between points.

You just have to build better what you want to graph. The plot function (x, y) requires the points x and y, and joins them with lines, for example if I write plot([1, 3], [6, 8]) plot a line joining the point (1, 6) Point (3, 8)

import matplotlib.pyplot as plt

datax1=[0,0,0,0,0]
datay1=[1,2,3,4,5]

datax2=[1,1,1,1,1]
datay2=[1,4,9,16,25] 

for i in range(len(datax1)):
    x = (datax1[i], datax2[i])
    y = (datay1[i], datay2[i]) 
    plt.plot(x, y)

plt.show()

在此处输入图片说明

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