Python 是否有更好的方法将这些标签与我使用matplotlib创建的绘图中的线条对齐?
这是我的代码,到目前为止,输出是好的,但我认为我可以更好地修改行“1-2”…“8-1”的标签。有没有办法使它们更适合生产线的每个部分? 我找到了一个,但它是一个代码,为每一个新行提供标签,但我的代码有一个不同点的连续行Python 是否有更好的方法将这些标签与我使用matplotlib创建的绘图中的线条对齐?,python,python-3.x,pandas,matplotlib,subplot,Python,Python 3.x,Pandas,Matplotlib,Subplot,这是我的代码,到目前为止,输出是好的,但我认为我可以更好地修改行“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,
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')
这是它给出的输出您的代码不起作用。你应该发布一个代码works@LaurentB. 它在jupyter笔记本上运行得很好?我想说的是,为了快速调试,里面的数据最少,就像一个没有数据集的笔记本一样