matplotlib的字体设置爬坑(坐标字体)

本文介绍了在matplotlib中设置字体时遇到的问题及解决方案。通过示例展示了如何正确修改坐标轴刻度的字体属性,包括使用`fontproperties`和`font`参数的区别,以及`fontsize`设置的注意事项。
摘要由CSDN通过智能技术生成

涉及到中文时font与fontproperties的区别

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
#首先导入依赖库

然后简单地画一张图:

font=FontProperties(fname=r"C:\Windows\Fonts\FZSTK.TTF",size=24)
fig, ax = plt.subplots(1,1,figsize=(8,8))
x = np.linspace(0.01,10,300)
y1 = x-2
y2 = np.log(x)
ax.plot(x,y1)
ax.plot(x,y2)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_position
matplotlib中,您可以使用不同的方法来设置字体。引用中的方法一是通过导入matplotlib库并设置字体的参数来实现。具体步骤是: 1. 导入matplotlib库:import matplotlib 2. 设置字体:font = {'family':'SimHei', 'weight':'bold', 'size':12} 3. 应用字体设置matplotlib.rc("font", **font) 另一种方法是在画图时自定义字体格式。引用中的方法是: 1. 导入matplotlib库:from matplotlib import pyplot as plt 2. 定义图形和坐标轴:fig, ax = plt.subplots() 3. 修改全局字体:ax.set_title("标题", fontname="Times New Roman") 还有一种方法是通过使用matplotlib.font_manager模块来设置字体。引用中的方法三是: 1. 导入matplotlib库:from matplotlib import pyplot as plt 2. 导入字体管理器:from matplotlib.font_manager import FontProperties 3. 定义字体:font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) 4. 绘制图形:x = [1,2,3,4] y = [3,2,1,3] 5. 添加x轴标签并设置字体:plt.xlabel("x轴标签",fontproperties=font) 6. 显示图形:plt.show() 这些是设置matplotlib字体的几种方法,您可以根据需要选择适合您的情况的方法来设置字体。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python碎片|matplotlib设置中文字体的三种方法](https://blog.csdn.net/wglink/article/details/113263089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [matplotlib添加字体字体格式自定义](https://blog.csdn.net/jasminefeng/article/details/120834650)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值