话不多说直接看代码
from matplotlib import pyplot as plt
def read_sharefile_datas():
"""读取数据"""
with open("./shares_data.csv", "r", encoding="utf-8") as file:
datas = file.readlines()[1:]
# 容器
dates = []
open_prices = []
close_prices = []
highest_prices = []
lowest_prices = []
for line in datas:
line = line[:-1]
day_list = line.split(",")
dates.append(day_list[0])
open_prices.append(float(day_list[1]))
close_prices.append(float(day_list[4]))
highest_prices.append(float(day_list[2]))
lowest_prices.append(float(day_list[3]))
return dates, open_prices, close_prices, highest_prices, lowest_prices
def main():
# 设置字体
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
# 画板
plt.figure(figsize=(16, 10), dpi=100)
# 读取csv文件
dates, open_prices, close_prices, highest_prices, lowest_prices = read_sharefile_datas()
# 绘制折线图
# 当天开盘价
plt.plot(dates, open_prices, "g-o", label="当天开盘价")
# 当天闭盘价
plt.plot(dates, close_prices, "g--", label="当天闭盘价")
# 当天最高价
plt.plot(dates, highest_prices, "r-s", label="当天最高价")
# 当天最低价
plt.plot(dates, lowest_prices, "r--", label="当天最低价")
# 绘制闭盘价最高点
day_highest = max(close_prices)
day_lowest = min(close_prices)
high_text = "当天闭盘最高点" + str(day_highest)
low_text = "当天闭盘最低点" + str(day_lowest)
plt.text(close_prices.index(day_highest), day_highest, high_text, color="green")
# 绘制闭盘最高点
plt.text(close_prices.index(day_lowest), day_lowest, low_text, color="green")
# 设置x刻度
plt.xticks([i for i in range(len(dates))], dates, rotation=40)
# 设置y刻度
plt.yticks([i for i in range(3050, 3420, 10)])
# 限制x轴刻度
plt.xlim(-1, len(dates))
# 限制y轴刻度
plt.ylim(3040, 3430)
# 网格
plt.grid(alpha=0.3)
# 标题
plt.title("某股票2015年1月~3月股票变化趋势图")
# x轴说明
plt.xlabel("时间日期")
# y轴说明
plt.ylabel("股价 (单位:元)")
# 图例
plt.legend(loc="upper right")
# 显示
plt.show()
if __name__ == '__main__':
main()
csv文件:
date,open_price,highest_price,lowest_price,close_price,turnover,total_price
2015/1/5,3258.627,3369.281,3350.519,3253.883,53135238400,549760073728.000
2015/1/6,3330.799,3394.224,3351.446,3303.184,50166169600,532398473216.000
2015/1/7,3326.649,3374.896,3373.954,3312.211,39191888000,436416708608.000
2015/1/8,3371.957,3381.566,3293.456,3285.095,37113116800,399230304256.000
2015/1/9,3276.965,3404.834,3285.412,3267.509,41024086400,458648027136.000
2015/1/12,3258.213,3275.185,3229.316,3191.582,32206467200,366273069056.000
2015/1/13,3223.542,3259.386,3235.301,3214.412,23072576000,273588781056.000
2015/1/14,3242.337,3268.483,3222.437,3193.978,24019075200,267204526080.000
2015/1/15,3224.07,3337.084,3336.455,3207.545,28254624000,330610540544.000
2015/1/16,3343.603,3400.318,3376.495,3340.49,33987676800,392253898752.000
2015/1/19,3189.727,3262.207,3116.351,3095.066,40109878400,409885999104.000
2015/1/20,3114.56,3190.245,3173.052,3100.477,35708080000,416295223296.000
2015/1/21,3189.085,3337.004,3323.611,3178.343,41095603200,473758662656.000
2015/1/22,3327.319,3352.384,3343.344,3293.978,35338297600,407874076672.000
2015/1/23,3357.096,3406.786,3351.764,3328.293,36624924800,420979539968.000
2015/1/26,3347.257,3384.798,3383.182,3321.315,31754099200,358427459584.000
2015/1/27,3389.853,3390.216,3352.96,3290.219,37451753600,418298855424.000
2015/1/28,3325.722,3354.802,3305.738,3294.655,30192710400,341564293120.000
2015/1/29,3258.997,3286.786,3262.305,3234.241,27465862400,296424505344.000
2015/1/30,3273.747,3288.503,3210.363,3210.308,25831254400,284265644032.000
2015/2/2,3148.136,3175.134,3128.3,3122.572,25086163200,266849959936.000
2015/2/3,3156.086,3207.935,3204.907,3129.732,24819216000,283355938816.000
2015/2/4,3212.822,3238.982,3174.126,3171.144,24909808000,290155167744.000
2015/2/5,3251.212,3251.212,3136.531,3135.819,30613929600,348266954752.000
2015/2/6,3120.09,3129.539,3075.907,3052.939,24674966400,266502782976.000
2015/2/9,3063.51,3119.034,3095.124,3049.111,20610838400,240719675392.000
2015/2/10,3090.49,3142.099,3141.593,3084.253,19381713600,225084915712.000
2015/2/11,3145.765,3166.421,3157.704,3139.052,17284009600,210862555136.000
2015/2/12,3157.959,3181.766,3173.416,3134.244,19459230400,229691572224.000
2015/2/13,3186.808,3237.159,3203.827,3182.794,26129043200,293017681920.000
2015/2/16,3206.137,3228.846,3222.363,3195.884,22379742400,265950691328.000
2015/2/17,3230.884,3255.725,3246.906,3230.772,22833262400,263340048384.000
2015/2/25,3256.479,3257.223,3228.843,3215.552,23334809600,265143353344.000
2015/2/26,3222.151,3300.616,3298.359,3202.188,30126387200,334347468800.000
2015/2/27,3296.831,3324.546,3310.303,3291.007,29916371200,335019573248.000
2015/3/2,3332.721,3336.76,3336.285,3298.669,34644566400,410259554304.000
结果长这样: