Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Connecting two x variables with no continuous line with matplotlib

could someone please help me to solve my problem? I have a dataframe with x1, x2 and y. The x's represent the indexes i want to connect. The y is the high of the plot (could also be some other value). I have no idea how to connect the points with matplotlib.

import pandas as pd
df = pd.DataFrame({"x1":[1,5,8,10], "x2":[3,6,9,13], "y":[1,1,1,1]})

Should look something like this.
enter image description here

Thank you for your help!

question from:https://stackoverflow.com/questions/65835400/connecting-two-x-variables-with-no-continuous-line-with-matplotlib

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

1 Answer

0 votes
by (71.8m points)

zip is your friend:

import matplotlib.pyplot as plt

import pandas as pd
df = pd.DataFrame({"x1":[1,5,8,10], "x2":[3,6,9,13], "y":[1,3,2,4]})

plt.plot(*zip((df.x1, df.y), (df.x2, df.y)), color="blue")

plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...