matplotlib库疑难问题---9、画箭头(综合实例)
matplotlib库疑难问题---9、画箭头(综合实例)
一、总结
一句话总结:
画箭头:ax.annotate("", xy=(x_list[i]-0.1, y2_mean), xytext=(x_list[i]-0.1, y2_list[i]),arrowprops=dict(arrowstyle="<->"))
annotate第一个参数是文本,xy表示起始位置,xytext表示文本的位置,arrowprops表示箭头的属性
二、画箭头(综合实例)
博客对应课程的视频位置:
9、画箭头(综合实例)-1-范仁义-读书编程笔记
https://www.fanrenyi.com/video/43/391
9、画箭头(综合实例)-2-范仁义-读书编程笔记
https://www.fanrenyi.com/video/43/392
一、实例
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
def show_img(x_data,y1_data,y2_data,annotate_num=0):
# 设置matplotlib库字体的非衬线字体为黑体
plt.rcParams["font.sans-serif"]=["SimHei"]
# 设置matplotlib库字体族为非衬线字体
plt.rcParams["font.family"]="sans-serif"
fig, ax = plt.subplots()
# 取消边框
for key, spine in ax.spines.items():
# 'left', 'right', 'bottom', 'top'
if key == 'left' or key == 'right':
spine.set_visible(False)
plt.xticks(np.arange(6), ('','t1', 't2', 't3', 't4', 't5'))
plt.yticks([])
# 上部分图
x_mean=[0,2,3,4,7]
#x_list=[1,2,3,4,5]
x_list=x_data
x=np.array(x_list)
# [19,14,16,18,13]
y=np.array(y1_data)
y_mean=np.mean(y)
y_mean_list=y_mean.repeat(5)
#plt.plot(x,y,'ro')
plt.plot(x,y,color='red', marker='o', linestyle='dashed',linewidth=0, markersize=12)
plt.plot(x_mean,y_mean_list,'k--')
x_smooth = np.linspace(x.min(),x.max(),300) #300 represents number of points to make between T.min and T.max
y_smooth = make_interp_spline(x, y)(x_smooth)
plt.plot(x_smooth,y_smooth,'r--')
plt.text(-