简体   繁体   中英

How to plot a new line between the markers on two separate plot lines using matplotlib?

I have created a graph that currently shows two plots (markers only) for each index. I want to create a third plot (with lines) connecting the two previous entries.

How the plot currently looks:

在此处输入图像描述

How can I plot a line to connect each items red dot to the blue dot?

My current code to draw the plot looks like this:

plt.figure(figsize=(8,7))
plt.plot(points_vs_xpoints["xpts"],points_vs_xpoints.index, label="Projected", linestyle = 'None', marker="o")
plt.plot(points_vs_xpoints["pts"], points_vs_xpoints.index, label="Actual", linestyle = 'None', marker="o", markeredgecolor="r", markerfacecolor='None')

plt.xticks(np.arange(0, 100, 10))
plt.xticks(rotation=90)
plt.legend()
plt.grid(color='grey', linewidth=0.2)
plt.tight_layout()
plt.show()

If I understood your request correctly, this should do the trick.

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

plt.figure(figsize=(8,7))
f, ax = plt.subplots(1, 1)
ax.plot(points_vs_xpoints["xpts"],points_vs_xpoints.index, label="Projected", linestyle = 'None', marker="o")
ax.plot(points_vs_xpoints["pts"], points_vs_xpoints.index, label="Actual", linestyle = 'None', marker="o", markeredgecolor="r", markerfacecolor='None')

lines = LineCollection([[[el[0], points_vs_xpoints.index[i]], [el[1], points_vs_xpoints.index[i]]] for i, el in enumerate(zip(points_vs_xpoints["xpts"],points_vs_xpoints["pts"]))], label='Connection', linestyle='dotted')
ax.add_collection(lines)

ax.set_xticks(np.arange(0, 100, 10))
plt.tick_params(rotation=90)
ax.legend()
ax.grid(color='grey', linewidth=0.2)
f.tight_layout()
plt.show()

See the resulting graph here

Edit

I wrongly assumed your index was a numeric one. With a string index, you can try something like this

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

plt.figure(figsize=(8,7))
f, ax = plt.subplots(1, 1)
ax.plot(points_vs_xpoints["xpts"],points_vs_xpoints.index, label="Projected", linestyle = 'None', marker="o")
ax.plot(points_vs_xpoints["pts"], points_vs_xpoints.index, label="Actual", linestyle = 'None', marker="o", markeredgecolor="r", markerfacecolor='None')

# Let's go with a simple index
lines = LineCollection([[[el[0], i], [el[1], i]] for i, el in enumerate(zip(points_vs_xpoints["xpts"],points_vs_xpoints["pts"]))], label='Connection', linestyle='dotted')
ax.add_collection(lines)

# Change for labels rotation
ax.set_xticklabels(np.arange(0, 100, 10), rotation=90)
ax.legend()
ax.grid(color='grey', linewidth=0.2)
f.tight_layout()
plt.show()

See the resulting graph here

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