我创建了一个图形,当前为每个索引显示两个图(仅用于标记)。 我想创建连接前两个条目的第三个图(带线)。
该地块目前的外观:
如何绘制一条线以将每个项目的红点连接到蓝点?
我当前绘制绘图的代码如下:
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()
答案 0 :(得分:0)
如果我正确理解了您的请求,就可以解决问题。
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()
我错误地认为您的索引是数字索引。使用字符串索引,您可以尝试这样的事情
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()