首页 > 解决方案 > 有没有更好的方法将这些标签与我使用 matplotlib 创建的这个图中的线条对齐?

问题描述

这是我拥有的代码,到目前为止输出很好,但我认为我可以更好地修改行“1-2”,...“8-1”的标签。有没有办法让它们更适合生产线的每个部分?我找到了一个相关的帖子,但它是一个代码,它为每条新线提供标签,但我的有一条带有不同点的连续线

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("data.txt",sep='[\t,]', engine='python')
print(data)

fig, ax = plt.subplots()
data.plot(x="Eastings", y="Northings", ax=ax, color = "violet", legend=None)
ax.plot(data.iloc[[0, -1]]['Eastings'], data.iloc[[0, -1]]['Northings'], color='violet')
data.plot.scatter(x="Eastings", y="Northings", ax=ax, color ="purple")

station_list = data["Station"].values.tolist()
x = data["Eastings"].values.tolist()
y = data["Northings"].values.tolist()

stat_lines = [] #making line labels such as "1-2", "2-3" ... "8-1"
for l in range(1,len(station_list)+1):
    if l == (len(station_list)):
        line = str(l) + '-' +str(l-(len(station_list)-1))
        stat_lines.append(line)
    else:
        line = str(l) + '-' + str(l+1)
        stat_lines.append(line)

for i, txt in enumerate(station_list):
    plt.annotate(txt, (x[i], y[i]), size=15, xytext=(1,5), ha='center', textcoords='offset points', color = "blue")

for i, txt in enumerate(stat_lines):
    plt.annotate(txt, (x[i]+5, y[i]+5), size=10, xytext=(1,5), ha='center', textcoords='offset points')

这是它给出的输出matplotlib 输出

标签: pythonpython-3.xpandasmatplotlibsubplot

解决方案


推荐阅读