python matplotlib笔记:折线图

1、折线图参数
x:x轴数据;
y:y轴数据;

linestyle:指定折线的类型,可以是实线、虚线、点虚线、点点线等,默认文实线;
linewidth:指定折线的宽度
marker:可以为折线图添加点,该参数是设置点的形状;
markersize:设置点的大小;
markeredgecolor:设置点的边框色;
markerfactcolor:设置点的填充色;
label:为折线图添加标签,类似于图例的作用;

2、 样例
读取股票数据

# 登陆系统 ####
lg = bs.login()
# 显示登陆返回信息
print('login respond error_code:' + lg.error_code)
print('login respond  error_msg:' + lg.error_msg)
# 需要格式如下:sh.601398
# 3 是不复权数据, 2 是前复权数据 一般使用前复权
start_date = '2023-01-01',
end_date = "2023-03-06",
adjustflag = "2"
rs = bs.query_history_k_data_plus("sh.601398",
                                  "code,date,isST,tradestatus,pctChg,preclose,open,high,low,close,volume,amount,turn,peTTM,pbMRQ,psTTM,pcfNcfTTM",
                                  start_date='2020-01-01', end_date="2023-03-06",
                                  frequency="d", adjustflag='2')
print('query_history_k_data_plus respond error_code:' + rs.error_code)
print('query_history_k_data_plus respond  error_msg:' + rs.error_msg)

#### 打印结果集 ####
data_list = []
while (rs.error_code == '0') & rs.next():
    # 获取一条记录,将记录合并在一起
    data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)

数据如下所示:
在这里插入图片描述
画图代码:

# 设置绘图风格
plt.style.use("ggplot")
# 设置中文编码和符号的正常显示
# 修改字体
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams["axes.unicode_minus"] = False

# 设置图框的大小
fig = plt.figure(figsize = (10,6))
# 绘图
plt.plot(result[-20:].date, # x轴数据
         result[-20:].close, # y轴数据
         linestyle = '-', # 折线类型
         linewidth = 2, # 折线宽度
         color = 'red', # 折线颜色
         marker = 'o', # 点的形状
         markersize = 2, # 点的大小
         markeredgecolor='black', # 点的边框色
         markerfacecolor='black') # 点的填充色
# 添加标题和坐标轴标签
plt.title('sh.601398收盘价趋势图')
plt.xlabel('日期')
plt.ylabel('收盘价')

# 剔除图框上边界和右边界的刻度
plt.tick_params(top = 'off', right = 'off')

# 为了避免x轴日期刻度标签的重叠,设置x轴刻度自动展现,并且45度倾斜
fig.autofmt_xdate(rotation = 45)

# 显示图形
plt.show()

在这里插入图片描述

参考:
Pandas——Matplotlib绘制折线图

猜你喜欢

转载自blog.csdn.net/weixin_39747882/article/details/128436659