简体   繁体   English

在图形中注释兴趣点(Matplotlib)

[英]Annotating a point of interest in a graph(Matplotlib)

I want to annotate and draw a line at a point of interest in my graph. 我想注释并在图中的兴趣点画一条线。 I have written a code for it, which does the work. 我已经为此编写了代码,可以完成工作。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fsource=('/Users/Maxwell/Desktop/OES data.xlsx')
df=pd.read_excel(fsource,header = 1, parse_cols = "C:D",names=['Wavelength','Intensity'])
fig1 = plt.figure()
f, ax = plt.subplots(1, figsize = (8,4))
ax.set_xlabel('Wavelength',fontsize=15)
ax.set_ylabel('Intensity',fontsize=15)
ax.plot(df['Wavelength'],df['Intensity'],color='red')
ax.set_xlim(xmin=400,xmax=800)
ax.set_ylim(ymin=0,ymax=5000) #Specify the max and min of y axis
ax.tick_params(axis= 'both', labelsize=12) #fontsize of x & y ticks as 12
ax.axvline(x= 604,color='black',ymin=0.05, ymax=0.5) #x=604 is my point of interest.
ax.annotate('POF-1', xy=(604, 2400),fontsize=12)
plt.show()

I have some questions regarding this method/procedure: 我对此方法/程序有一些疑问:

  1. Is there a more efficient/better way of doing the same work?(any refinements to code etc.) 是否有更有效/更好的方法来完成相同的工作?(对代码进行的任何改进等)

  2. My code works for only one point of interest 'POF-1'. 我的代码仅适用于一个兴趣点“ POF-1”。 If I have several points of interest, I will have to number them one by one and also specify the length of line again and again. 如果我有多个兴趣点,则必须将它们逐一编号,并一次又一次指定线的长度。 What should I do to automate the task, such that annotation and lines appear at all points of interest at the same time?(for eg annotate points x=500, 730, 790 etc with some predefined text.) 我应该怎么做才能使任务自动化,以使注释和线条同时出现在所有感兴趣的点上?(例如,用一些预定义的文本注释点x = 500、730、790等)。

The link for the raw file is OES data in the link 原始文件的链接是链接中的OES数据

You may loop over the points to annotate: 您可以遍历要注释的点:

xs = [500, 730, 790]
texts = ["POF-1", "Some peak", "another peak"]
for x, t in zip(xs, texts):
    ax.axvline(x=x, color='black',ymin=0.05, ymax=0.5)
    ax.annotate(t, xy=(x, 2400),fontsize=12)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM