I often want to highlight a point along a curve using matplotlib
to make a plot that looks like:
以下代码用于创建绘图
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
y = np.array([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100])/100.
x = 100. - np.array([
99.79,98.96,98.65,98.39,98.13,97.88,97.61,97.33,97.01,96.65,96.21,
95.72,95.16,94.46,93.52,92.31,90.66,88.48,84.04,79.34,19.32])
ax = plt.subplot(111)
line = plt.plot(x,y)
def highlight_point(ax,line,point,linestyle=':'):
c = line.get_color()
xmin = ax.get_xlim()[0]
ymin = ax.get_ylim()[0]
ax.plot([xmin,point[0]],[point[1],point[1]],color=c,linestyle=linestyle)
ax.plot([point[0],point[0]],[ymin,point[1]],color=c,linestyle=linestyle)
plt.xlim([0,85])
plt.ylim([0,1])
highlight_point(ax,line[0],[x[10],y[10]])
plt.show()
当上述方法失败时xlim
and ylim
输入失败或者稍后将另一个图添加到图中。我想要一些组合axhline
or hlines
我可以在其中将图的左侧/底部指定为某个数学点。