本次学习了《科研论文配图绘制指南—— 基于Python 》的第二章,Matplotlib是Python中较为常用和知名的可视化绘图工具。
Matplotlib图形元素
画布(figure) | 画布大小(figsize) |
分辨率(dpi) | |
背景颜色 | |
坐标图形(axes)---子图 | 提供绘图展示区域 |
轴(axis) | 位置(locator)对象 |
格式(formatter) | |
刻度(tick) | 主刻度(Major tick) |
次刻度(Minor tick) | |
主刻度标签(Major tick label) | |
次刻度标签(Minor tick label) |
Matplotlib 库绘制的图形的主要构成元素
图层顺序
Matplotlib 可通过设置每个绘图函数中的zorder 参数来设定不同的图层。
这一部分没怎么理解,不知道具体操作。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.text import TextPath
from matplotlib.transforms import Affine2D
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.patches import Rectangle, PathPatch
plt.rcParams["font.family"] = "Times New Roman"
colors = ["#2FBE8F","#459DFF","#FF5B9B","#FFCC37","#751DFE","#0057FF"]
def text3d(ax, xyz, s, zdir="z", size=None,angle=0, **kwargs):
x, y, z = xyz
if zdir == "y":
x, y, z = x, z, y
elif zdir == "x":
x, y, z = y, z, x
else:
x, y, z = x, y, z
text_path = TextPath((0, 0), s, size=size)
trans = Affine2D().rotate(angle).translate(x, y)
p = PathPatch(trans.transform_path(text_path), **kwargs)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=z, zdir=zdir)
fig = plt.figure(figsize=(8, 5),dpi=100,facecolor="w")
ax = fig.add_subplot(111, projection="3d", xticks=[], yticks=[], zticks=[])
ax.set_axis_off()
ax.set_xlim(0, 10), ax.set_ylim(0, 10), ax.set_zlim(0, 10)
figs = ["Figure (background)",
"Images(zorder=0)",
"Patches (zorder=1)",
"Lines (zorder=2)",
"Text (zorder=3)",
"Inset axes & legend (zorder=5)",
]
for i, text,color in zip(np.arange(len(figs)),figs,colors):
#p = Rectangle((0, 0), 10, 10, edgecolor="None", facecolor="white", alpha=0.5)
p = Rectangle((0, 0), 10, 10, edgecolor="None", facecolor=color, alpha=0.5)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=i, zdir="z")
p = Rectangle((0, 0), 10, 10, edgecolor="black", facecolor="None",lw=.6)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=i, zdir="z")
text3d(
ax,
(-0.25, 0.25, i),
text,
zdir="z",
size=0.6,
angle=np.pi / 2,
ec="none",
fc=color,
)
plt.tight_layout()
plt.savefig(".\图2-1-2 Matplotlib 各Aritist对象默认图层顺序示意图.png",
bbox_inches='tight',dpi=300)
plt.show()
#
import pandas as pd
import numpy as np
import proplot as pplt
import matplotlib.pyplot as plt
from proplot import rc
rc["font.family"] = "Times New Roman"
rc['tick.labelsize'] = 12
rc["suptitle.size"] = 15
r = np.linspace(0.3, 1, 30)
theta = np.linspace(0, 4 * np.pi, 30)
x = r * np.sin(theta)
y = r * np.cos(theta)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.5), facecolor="w", sharey=True)
ax1.plot(x, y, 'C3', lw=4)
ax1.scatter(x, y, s=180, color="#FFCC37", ec="k", label="zorder=2")
ax1.set_title('Lines on top of dots', fontsize=15)
ax1.legend()
ax2.plot(x, y, 'C3', lw=4)
ax2.scatter(x, y, s=180, color="#FFCC37", ec="k", zorder=3, label="zorder=3") # move dots on top of line
ax2.set_title('Dots on top of lines', fontsize=15)
ax2.legend()
plt.tight_layout()
plt.savefig(".\图2-1-3 Matplotlib 修改zoerder参数图层顺序实例演示.png",
bbox_inches='tight',dpi=300)
plt.show()
坐标系
常见的坐标系有直角坐标系(rectangular coordinate system)、极坐标系(polar coordinate
system)和地理坐标系(geographic coordinate system),其中直角坐标系和地理坐标系在科研
论文绘图中出现的频率较高。
多子图绘制
subplot
Matplotlib提供的subplot()函数对画布添加单个子图。示例代码如下:
import matplotlib.pyplot as plt
ax1 = plt.subplot(212)
ax2 = plt.subplot(222)
ax3 = plt.subplot(221)
add_subplot
import matplotlib.pyplot as plt
fig = plt.figure() #与subplot的区别
ax1 = fig.add_subplot(212)
ax2 = fig.add_subplot(221)
ax3 = fig.add_subplot(222)
常见图片类型