实例运行代码如下:
# 需求一:绘制某股票一周内收盘价折线图(见运行结果展示图)
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
# 0.设置中文黑体
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# 1.准备数据
x_data = np.arange(1,8,1)
y_data = [44.98, 45.02, 44.32, 41.05, 42.08, 42.08, 42.08] # 周末休市
# 2.创建画布和坐标系
ax = plt.axes((0.2,0.3,0.5,0.5))
# 3.绘图
ax.plot(x_data,y_data,'mo-') #绘制折线图(绘制紫色带圆点的直线)
# 4.定制刻度(刻度线样式调整为方向朝内,宽度为2,长度为8;以下两种方法均可实现)
#ax.tick_params(axis='x',direction = 'in',width = 2,length = 8,labelrotation = 45)
#ax.tick_params(axis='y',direction = 'in',width = 2,length = 8)
ax.xaxis.set_tick_params(direction = 'in',width = 2,length = 8,labelrotation = 45)
ax.yaxis.set_tick_params(direction = 'in',width = 2,length = 8)
#设置x轴的标签
ax.set_xticks([1,2,3,4,5,6,7])
ax.set_xticklabels(['周一','周二','周三','周四','周五','周六','周日'])
# 5.设置轴标签(刻度标签)
ax.set_xlabel('周日期')
ax.set_ylabel('收盘价(¥)')
# 6.隐藏上轴脊和右轴脊
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# 7.展示图表
plt.show()
运行结果如下: