Matplotlib绘图添加颜色元素

文章介绍了如何在Matplotlib中更改默认画板颜色,设置字体,以及提供多种背景和配色选项,以创建股票图表实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 Matplotlib默认画板颜色为白色

import matplotlib.pyplot as plt
import matplotlib as mpl

font_name = "YouYuan"
mpl.rcParams['font.family']=font_name  # 设置字体
mpl.rcParams['axes.unicode_minus']=False  # 解决“-”的乱码问题

daima_name = {'301487': 'N盟固利', '309674': '*股票名称', '002310': '东方园林'}
daima = '309674'
name_1 = daima_name[daima]
name = daima + "—"+ name_1

y_list = [29.67, 26.33, 26.01, 24.53, 24.5, 23.98, 23.79, 24.12, 23.77, 23.43, 23.75, 24.21, 23.87, 23.45, 23.88, 24.16, 24.0, 24.01, 23.61, 22.77, 22.82]
x_list = [x  for x  in range(1,len(y_list)+1)]

for x, y, z in zip(x_list, y_list,y_list):
    plt.text(x, y,z, ha='left', va='bottom',fontsize=12,fontweight = "bold",verticalalignment = "baseline") 
    plt.plot(x_list, y_list, 'ro--', alpha=0.5,linewidth = '1', linestyle = 'solid') 
    plt.title(name, fontsize=13,color="blue",fontweight = "bold") # 本行显示默认字体

plt.show()

在这里插入图片描述

二 给画板设置颜色

只需要添加两行代码,如下所示:

import matplotlib as mpl
import matplotlib.font_manager

#print([f.name for f in matplotlib.font_manager.fontManager.ttflist])
font_name = "YouYuan"
mpl.rcParams['font.family']=font_name
mpl.rcParams['axes.unicode_minus']=False

# 以下两行代码
plt.rcParams['axes.facecolor'] = 'azure'        #设置绘图背景色
plt.rcParams['figure.facecolor'] = 'lavenderblush'      #设置背景色


datetime_now_str = datetime.datetime.now().strftime('%Y-%m-%d')
daima_name = {'301487': 'N盟固利', '309674': '股票名称', '002310': '东方园林'}
daima = '309674'
name_1 = daima_name[daima]
name = daima + " — "+ name_1 + " — " + datetime_now_str 

y_list = [29.67, 26.33, 26.01, 24.53, 24.5, 23.98, 23.79, 24.12, 23.77, 23.43, 23.75, 24.21, 23.87, 23.45, 23.88, 24.16, 24.0, 24.01, 23.61, 22.77, 22.82]
x_list = [x  for x  in range(1,len(y_list)+1)]

for x, y, z in zip(x_list, y_list,y_list):
    plt.text(x, y,z, ha='left', va='bottom',fontsize=12,fontweight = "bold",verticalalignment = "baseline") # 本行应该显示默认字体
    plt.plot(x_list, y_list, 'ro--',alpha=0.5,linewidth = '1', linestyle = 'solid') 
    plt.title(name, fontsize=13,color="blue",fontweight = "bold")

plt.show()

在这里插入图片描述

三 多种配色选择

plt.rcParams['axes.facecolor'] = 'azure'        #设置绘图背景色
plt.rcParams['figure.facecolor'] = 'lavenderblush'      #设置背景色
# 其中 = 右侧的字符串代表颜色类型,下表会给出对应的颜色及名称,根据自己喜好修改即可

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力当总裁的郭琛予

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值