【Matplotlib】探究Matplotlib的设计之美

发布于:2022-08-05 ⋅ 阅读:(557) ⋅ 点赞:(0)

Matplotlib的设计之美

1.创建一个(或多个)图表

创建一个图表

plt.plot()

创建多个图表

方法一:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, layout='constrained')

方法二:

plt.subplot(2, 1, 1)	# ax1
plt.subplot(2, 1, 2)	# ax2
plt.subplot(2, 2, 1)	# ax3
plt.subplot(2, 2, 2)	# ax4

还可以加入share_x=True/False, share_y=True/False来决定是否共享x/y轴

方法一的图表标题和x/y轴标签设置

ax1.set_title('Time Domain Signal')
ax1.set_xlabel('Time')
ax1.set_ylabel('Amplitude')
# 依此类推

2. Axis标签位置

ax.set_ylabel('轴名称', loc='top') # loc可选'bottom', 'center', 'top'

Axis_Label_Position

3.基本绘图样式

# Basic
"""基本绘图类型,通常是 y 与 x。"""
ax.plot(x, y)	# 曲线图
ax.scatter(x, y)	# 散点图
ax.bar(x, height)/barh(y, width)	# 直方图
ax.stem(x, y)	# 茎蔓图
ax.step(x, y)	# 步骤图/阶梯图
ax.fill_between(x, y1, y2)	# 填充y1, y2之间

# 数组和字段图
"""绘制数据数组 Z(x, y) 和字段 U(x, y)、V(x, y)。"""
ax.imshow(Z)	# 在x,y构建的坐标图上显示
ax.pcolormesh(X, Y, Z)	# pcolormesh比imshowx 和 y 向量不需要等距(实际上它们可以倾斜)更灵活。
ax.contour(X, Y, Z)	# 线条轮廓图
ax.contourf(X, Y, Z)	# 阴影轮廓图
ax.barbs(X, Y, U, V)	# 倒钩图
ax.quiver(X, Y, U, V)	# 颤动图
ax.streamplot(X, Y, U, V)	# 流图

"""统计分析图"""
ax.hist(x)	# 直方瀑布图
ax.boxplot(X)	# 箱线图
ax.errorbar(x, y, yerr, xerr)	# 误差条
ax.violinplot(D)	# 小提琴图
ax.eventplot(D)		# 事件图
ax.hist2d(x, y)		# 2D瀑布图
ax.hexbin(x, y, C)	# 蜂巢图
ax.pie(x)	# 饼图

"""非结构化坐标图"""
ax.tricontour(x, y, z)
ax.tricontourf(x, y, z)
ax.tripcolor(x, y, z)
ax.triplot(x, y)

4.线条样式

linestyle_str = [
     ('solid', 'solid'),      # Same as (0, ()) or '-'
     ('dotted', 'dotted'),    # Same as (0, (1, 1)) or ':'
     ('dashed', 'dashed'),    # Same as '--'
     ('dashdot', 'dashdot')]  # Same as '-.'

linestyle_tuple = [
     ('loosely dotted',        (0, (1, 10))),
     ('dotted',                (0, (1, 1))),
     ('densely dotted',        (0, (1, 1))),

     ('loosely dashed',        (0, (5, 10))),
     ('dashed',                (0, (5, 5))),
     ('densely dashed',        (0, (5, 1))),

     ('loosely dashdotted',    (0, (3, 10, 1, 10))),
     ('dashdotted',            (0, (3, 5, 1, 5))),
     ('densely dashdotted',    (0, (3, 1, 1, 1))),

     ('dashdotdotted',         (0, (3, 5, 1, 5, 1, 5))),
     ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
     ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]

lineStyles

5.标记(marker)参考

一共有四类:未填充标记、填充标记、利用TEX符号创建的标记以及从路径创建的标记

unfilled maekers

Filled markers

Mathtext markers

Path markers

此外,填充标记还可以选择填充样式:

Marker fillstyle

6.颜色设置

Matplotlib有九种设置颜色的格式:

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0.0, 2.0, 201)
s = np.sin(2 * np.pi * t)

# 1) RGB tuple:
fig, ax = plt.subplots(facecolor=(.2, .22, .16))
# 2) hex string:
ax.set_facecolor('#424c50')
# 3) gray level string:
ax.set_title('Voltage vs. time chart', color='0.944')
# 4) single letter color string
ax.set_xlabel('time (s)', color='c')
# 5) a named color:
ax.set_ylabel('voltage (mV)', color='DarkSeaGreen')
# 6) a named xkcd color:
ax.plot(t, s, 'xkcd:grey green')
# 7) Cn notation:
ax.plot(t, .7*s, color='C8', linestyle='-.')
# 8) tab notation:
ax.tick_params(labelcolor='tab:orange')


plt.show()

ColorSettings

效果如图所示。

附上颜色索引网址(特别好看,还有国风哟):

XKCD Color

国风传统颜色

X11/CSS4 Color

结语

总之,要想画出有趣好看的图表,还需要多多练习和探索哦~
随缘出第二期。


微信公众号

今日签到

点亮在社区的每一天
去签到