0%

Python | matplotlib

Matplotlib

Matplotlib Tutorials

.axes.Axes 轴格式

.set_major_formatter()

设置数据标签的显示的文本格式

1
2
3
4
5
ticks = np.arange(0., 8.1, 2.)
ax.xaxis.set_ticks(ticks)
ax.xaxis.set_major_formatter('{x:1.1f}') ## 显示为1位小数

ax.xaxis.set_major_formatter('±{x}°') ## 数字前带±号、数字后带°

.set_xlim()

设置x轴的数据范围

.set_ticks()

设置轴的刻度线

  • .xaxis.set_ticks():设置x轴刻度线
  • .yaxis.set_ticks():设置y轴刻度线

.set_ticklabels()

设置轴的数据标签格式

1
2
3
ticks = np.arange(0., 8.1, 2.)
ax.xaxis.set_ticks(ticks)
ax.xaxis.set_ticklabels([f'{tick:1.2f}' for tick in ticks])

.set_title()

设置图表标题

  • 参数loc:标题位置
    • left:靠左
    • center:居中
    • right:靠右
  • 参数pad:标题相对图表偏移(默认为6)
1
2
3
4
5
6
fig, axs = plt.subplots(3, 1, figsize=(5, 6), tight_layout=True)
locs = ['center', 'left', 'right']
for ax, loc in zip(axs, locs):
ax.plot(x1, y1)
ax.set_title('Title with loc at '+loc, loc=loc)
plt.show()

.set_xlabel()

修改$x$轴标签

matplotlib.axes.Axes.set_xlabel

  • 参数fontsize:设置标签字体大小
  • 参数fontweight:设备标签字体粗细
    • fontweight='bold':粗体
  • 参数fontproperties:设置字体
  • 参数horizontalalignment:设置标签水平对齐位置
    • horizontalalignment=left:水平向左对齐
  • 参数labelpad:标签相对轴偏移
  • 参数position=(a, b):标签位置
    • .set_xlabel()中,仅a起作用,b随便赋值
    • .set_ylabel()中,仅b起作用,a随便赋值
  • 标签换行:\n
  • 标签含公式:r'$公式内容$'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

x1 = np.linspace(0.0, 5.0, 100)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)

fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1)
ax.set_xlabel('time [s]')
ax.set_ylabel('Damped oscillation [V]')

plt.show()
1
2
3
4
5
6
7
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1*10000)
ax.set_xlabel('time [s]')
ax.set_ylabel('Damped oscillation [V]', labelpad=18)

plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from matplotlib.font_manager import FontProperties

font = FontProperties()
font.set_family('serif')
font.set_name('Times New Roman')
font.set_style('italic')

fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.15, left=0.2)
ax.plot(x1, y1)
ax.set_xlabel('time [s]', fontsize='large', fontweight='bold')
ax.set_ylabel('Damped oscillation [V]', fontproperties=font)

plt.show()
1
2
3
4
5
6
fig, ax = plt.subplots(figsize=(5, 3))
fig.subplots_adjust(bottom=0.2, left=0.2)
ax.plot(x1, np.cumsum(y1**2))
ax.set_xlabel('time [s] \n This was a long experiment')
ax.set_ylabel(r'$\int\ Y^2\ dt\ \ [V^2 s]$')
plt.show()

.set_ylabel()

修改$y$轴标签

例子见.set_xlabel()

.tick_params()

旋转刻度线标签,使得文本之间互相重叠

  • 参数rotation:逆时针旋转的度数
1
2
3
4
5
6
7
8
9
import datetime

fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
base = datetime.datetime(2017, 1, 1, 0, 0, 1)
time = [base + datetime.timedelta(days=x) for x in range(len(x1))]

ax.plot(time, y1)
ax.tick_params(axis='x', rotation=70)
plt.show()
1

1

参考资料

Thank you for your approval.

欢迎关注我的其它发布渠道