python - 有没有更好的方法将这些标签与我使用 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')
解决方案
推荐阅读
- python - Python:如何实现两个整数的交叉?
- c# - JsonConvert.Serialize 会自动压缩我的字符串还是应该使用 Gzip?
- python - 你如何抓取(使用 Python)一个不像表格那样格式化的网站?
- function - 递归函数如何在 MIPS 中工作?
- javascript - 在 ReactJS 中加载 WebP 图像
- python - Selenium & Beautiful Soup 在同一网站上返回不同的 len() 值刮(亚马逊)
- c# - 如何停止 String.Concat(); 从删除空格?
- angular - 登录后的角度刷新页面
- java - 如何查询 DynamoDB 并返回第一个匹配结果而不是查询整个集合?
- reactjs - this.state.backClick== 1 是什么意思?BackHandler.exitApp() : this._spring() 反应原生?