matplotlib笔记1 折线图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangguangdblu/article/details/84494375
# 导入库
import matplotlib.pyplot as plt
import numpy as np

# 生成x数组
x=np.linspace(-3,3,10)

# y的表达式
y1=np.sin(x)
y2=0.5*x+1

# 设置图的编号和大小
plt.figure(num=3,figsize=(8,5))

# 设置线形
plt.plot(x,y1,color='red',linewidth=0.75,linestyle='--',label='reality',marker='o')
plt.plot(x,y2,color='blue',linewidth=0.75,label='dream')

# 设置xy的范围
plt.xlim((-3,3))
plt.ylim((-1.5,1.5))

# 设置xy的坐标轴标签
plt.xlabel('time')
plt.ylabel('level')

# 设置刻度,并且可以重命名
new_ticks=np.linspace(-3,3,3)
plt.xticks(new_ticks,[r'yesterday',r'today','tomorrow'])
plt.yticks([-0.5,0,0.5],[r'awful',r'medium',r'success'])

# 设置边框
ax=plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 调整坐标轴位置,以及图标题
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',-3))
ax.set_title('test',fontsize='16',color='black')

# 增加图例
plt.legend(loc='upper right') # 'best','center','lower right' etc.

# 标注

# 画某点的垂线
x0 = 0.5
y0 = 0.5*x0 + 1
plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)

# 设置点的样式
plt.scatter([x0, ], [y0, ], s=50, color='b')

# 标注某一点
plt.annotate(r'$2x+1=%s$'% y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30),
             textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))

# 标注许多点
for xy in zip(x,y1):
    plt.annotate("(%.1f,%.1f)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points')

# 文本标注
plt.text(0.5, 1.5, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 12, 'color': 'r'})

# 设置刻度的透明度
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.7, zorder=2))

# 大功告成
plt.show()

运行结果
在这里插入图片描述

Python3.5+pip18.1+numpy1.15.4+matplotlib3.0.2

猜你喜欢

转载自blog.csdn.net/yangguangdblu/article/details/84494375
今日推荐