Python绘图——matplotlib

本文介绍了使用matplotlib进行基本的图形绘制,包括折线图的创建,以及如何调整图像的画布大小、设置图像分辨率、控制坐标轴的刻度间隔和范围、修改字体大小和样式,以及图例的位置设置。重点讲解了多个关键函数和参数的用法,如`figure(figsize)`、`xlim`、`ylim`、`xticks`、`yticks`、`set_xlabel`、`set_ylabel`和`legend`。

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

1. 导入包

import matplotlib.pyplot as plt

2. 基本绘图

(1)折线图

plt.plot(x,y)

3. 图像美化

(1)画布设置

# 设置画面大小
plt.figure(figsize=(6, 6.5))

# 设置图片分辨率
plt.rcParams['figure.dpi'] = 300 

(2)手动设置坐标轴刻度间隔以及刻度范围

#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
from matplotlib.pyplot import MultipleLocator

#ax为两条坐标轴的实例
ax=plt.gca()

#把x轴的刻度间隔设置为1,把y轴的刻度间隔设置为10
x_major_locator=MultipleLocator(1)
y_major_locator=MultipleLocator(10)
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)

# 把x轴的刻度范围设置为-0.5到11; 把y轴的刻度范围设置为-5到110
plt.xlim(-0.5,11)
plt.ylim(-5,110)

(3)调整字体大小

ax=plt.gca()

'''设置刻度字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
plt.xticks(fontsize=20,fontname='Times New Roman',weight = 'bold')
plt.yticks(fontsize=20,fontname='Times New Roman',weight = 'bold')

'''设置坐标标签字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
ax.set_xlabel(..., fontsize=20,fontname='Times New Roman',weight = 'bold')
ax.set_ylabel(..., fontsize=20,fontname='Times New Roman',weight = 'bold')

'''设置图例字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
ax.legend(..., fontsize=20,fontname='Times New Roman',weight = 'bold')

# 图例字体样式另一种设置方法
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 13,
}
plt.legend(prop=font1)

(4)设置legend位置

# 设置位置为左下
plt.legend(loc='lower left')

'''
左上:loc='upper left'
左下:loc='lower left'
右上:loc='upper right'
右下:loc='lower right'
'''

参考:

1. 画布设置:

(1)【数据展示】matplotlib设置画面大小_肥宅_Sean的博客-优快云博客_matplotlib设置画布大小

(2)Python绘图问题:Matplotlib中指定图片大小和像素_MeteorMan99的博客-优快云博客_matplotlib size

2. 刻度设置:

(1)Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围_宁宁Fingerstyle的博客-优快云博客_matplotlib 刻度

3. 调整字体大小

(1)https://blog.youkuaiyun.com/u011008379/article/details/60478927

(2)python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置_CAM-TAY的博客-优快云博客_matplotlib 字体(3)Matplotlib作图中坐标轴字体的加粗斜体格式_PascalXie的博客-优快云博客_matplotlib 字体加粗

4. legend 位置设置

(1)https://zhuanlan.zhihu.com/p/101059179

### Python Matplotlib 时间序列绘图不显示解决方案 当遇到Matplotlib时间序列绘图无法正常显示的情况时,通常是因为日期格式未被正确处理或配置不当所致。为了确保时间序列数据能够在图表上准确呈现,需遵循特定的数据准备和设置方法。 #### 数据预处理 对于时间序列数据而言,首先应确认所使用的日期/时间戳已被转换成`datetime`对象。这一步骤至关重要,因为Matplotlib内部依赖于这种标准的时间表示形式来计算刻度位置并渲染标签[^2]。 ```python import pandas as pd from datetime import datetime dates = ['2023-01-%d' % i for i in range(1, 6)] values = [i**2 for i in range(len(dates))] # 将字符串类型的日期转化为DatetimeIndex类型 date_index = pd.to_datetime(dates) ``` #### 设置X轴为时间格式 为了让X轴按照时间顺序排列,并且能够自动调整合适的刻度间距以及格式化输出年月日等信息,建议使用`DateFormatter`类来自定义日期样式,并通过`set_major_formatter()`应用到对应的坐标轴上[^3]。 ```python import matplotlib.pyplot as plt import matplotlib.dates as mdates fig, ax = plt.subplots() ax.plot(date_index, values) # 自动定位最佳的日期间隔 locator = mdates.AutoDateLocator() formatter = mdates.ConciseDateFormatter(locator) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) plt.xlabel('Date') plt.ylabel('Value') plt.title('Time Series Plot with Correct Date Display') plt.grid(True) plt.tight_layout() # 调整布局防止重叠 plt.show() ``` 上述代码片段展示了如何利用Pandas库中的`to_datetime()`函数将原始日期串转为适合Matplotlib操作的形式;接着借助`AutoDateLocator`与`ConciseDateFormatter`组合实现了动态适应不同范围内的日期展示效果,从而解决了可能出现的时间序列图像错位问题[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值