This page looks best with JavaScript enabled

科研画图

 ·  ☕ 3 min read

matplotlib

限制画布范围

只显示一定区域的图像,超出这个区域的图像不进行绘制

1
2
plt.xlim(xmin=-30,xmax=30)
plt.ylim(ymin=-30,ymax=30)

相同的比例尺

1
2
## https://stackoverflow.com/questions/17990845/how-to-equalize-the-scales-of-x-axis-and-y-axis-in-matplotlib
plt.gca().set_aspect('equal', adjustable='box')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## https://stackoverflow.com/questions/8130823/set-matplotlib-3d-plot-aspect-ratio
## 适用于Axes3d的三维画图
## extents 为画布范围

def axisEqual3D(ax):
    extents = np.asarray([[80,180],[20,120],[20,120]])

    sz = extents[:,1] - extents[:,0]
    centers = np.mean(extents, axis=1)
    maxsize = max(abs(sz))
    r = maxsize/2
    for ctr, dim in zip(centers, 'xyz'):
        getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)

fig = plt.figure()
ax = Axes3D(fig)
axisEqual3D(ax)

网格

1
plt.grid(True)

箭头

1
2
3
4
5
6
7
plt.arrow(x=0, y=0, dx=2, dy=-1,width=0.005,head_width=0.1,ec='r',fc='r')
plt.arrow(x=0, y=0, dx=-1, dy=2,width=0.005,head_width=0.1,ec='r',fc='r')

plt.xlim(xmin=-3,xmax=3)
plt.ylim(ymin=-3,ymax=3)
plt.grid(True)
plt.show()

也可以使用在标注过程中产生的箭头

1
2
3
4
5
6
plt.annotate("(2,-1)", xy=(2, -1), xytext=(0, 0), arrowprops=dict(arrowstyle="simple", color="r"))

plt.xlim(xmin=-3, xmax=3)
plt.ylim(ymin=-3, ymax=3)
plt.grid(True)
plt.show()

去掉边框

1
2
## 去掉坐标轴以及标签刻度
plt.axis("off")
1
2
3
4
5
ax = plt.gca()  # get current axis 获得坐标轴对象
ax.spines['right'].set_color('none')  # 将右边 边沿线颜色设置为空 其实就相当于抹掉这条边
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
1
2
3
4
## 适用于Axes3d的三维画图
fig = plt.figure()
ax = Axes3D(fig)
ax.set_axis_off()

中心在(0,0)的坐标轴

1
2
3
4
5
6
# 设置中心的为(0,0)的坐标轴
ax.spines['bottom'].set_position(('data', 0))  # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
# plt.xticks(rotation=45)#x轴数值倾斜45度显示
plt.xlim(-4.0, 5.0) #x轴数值设置
plt.ylim(-0.2, 0.5)

设置字体

1
2
from matplotlib import font_manager as fm, rcParams
rcParams['font.sans-serif'] = ['Times New Roman']

坐标轴科学计数法

1
2
3
4
5
6
from matplotlib.ticker import FuncFormatter
def formatnum(x, pos):
    return '%.2f×10$^{4}$' % (x/1e4)
formatter = FuncFormatter(formatnum)
# 设置坐标轴格式
plt.gca().yaxis.set_major_formatter(formatter)

ColorBar

https://stackoverflow.com/questions/58288579/how-to-get-a-lighter-jet-colormap-in-matplotlib

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import colorsys


def man_cmap(cmap, value=1.):
    colors = cmap(np.arange(cmap.N))
    hls = np.array([colorsys.rgb_to_hls(*c) for c in colors[:,:3]])
    hls[:,1] *= value
    rgb = np.clip(np.array([colorsys.hls_to_rgb(*c) for c in hls]), 0,1)
    return mcolors.LinearSegmentedColormap.from_list("", rgb)

cmap = plt.cm.get_cmap("jet")

fig, (ax1, ax2, ax3) = plt.subplots(3)
x=np.linspace(0,1,64)

sc = ax1.scatter(x,np.ones_like(x), c=x, cmap=cmap)
fig.colorbar(sc, ax=ax1, orientation="horizontal")

sc = ax2.scatter(x,np.ones_like(x), c=x, cmap=man_cmap(cmap, 0.75))
fig.colorbar(sc, ax=ax2, orientation="horizontal")

sc = ax3.scatter(x,np.ones_like(x), c=x, cmap=man_cmap(cmap, 1.25))
fig.colorbar(sc, ax=ax3, orientation="horizontal")

plt.show()

自定义颜色

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# colors = [ [30,70,110], [55,103,149],[82,143,173],[114,188,213],[170,220,224],[255,230,183],[255,208,111],[247,170,88], [239,138,71],[231,98,84]]
colors = [[49,124,183], [128,166,226],[251,221,133],[244,111,67],[207,67,62]]


# colors = [[49,124,183],[182,215,232],[233,241,244],[251,227,213],[246,178,147],[220,109,87],[183,34,48],[109,1,31]]

colors = [list(map(div, x))  for x in colors ]
cmap = mcolors.LinearSegmentedColormap.from_list("my_cmap", colors)
color = (np.exp(z) - np.exp(-1)) / (np.exp(1) - np.exp(-1))

# alpha controls opacity
ax.plot_surface(x, y, z, rstride=1, cstride=1,facecolors = cmap(color),linewidth=0.1, alpha=0.8)

其他没有的就使用scatter散点组成平面/线条

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.colors as mcolors

def div(x):
    return x / 255.


colors = [[49,124,183], [128,166,226],[251,221,133],[244,111,67],[207,67,62]]
colors = [list(map(div, x))  for x in colors ]
cmap = mcolors.LinearSegmentedColormap.from_list("my_cmap", colors)


kappa = 64
x = np.linspace(-np.pi,  np.pi, 5000)

## circle
cos = np.cos(x)
sin = np.sin(x)

plt.plot(cos,sin)

y = exp(cos)

plt.scatter(cos * (1 + y) , sin * (1 + y) ,  c=cmap(y) , edgecolor='none')

# plt.scatter(x, y )



plt.show()

几何画图

平面

已知法向量normal和空间中的一个点vector

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import math

## plot plane
w = 32
h = 32
xx, yy = np.meshgrid(range(math.floor(vector[0]) - w // 2, math.floor(vector[0]) + w // 2),
                        range(math.floor(vector[1]) - h // 2, math.floor(vector[1]) + h // 2))
d = -np.asarray(vector).dot(normal)
zz = (-normal[0] * xx - normal[1] * yy - d) * 1. / normal[2]

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xx, yy, zz, alpha=0.8)
plt.show()

Adobe AI

区域文字

标题栏“文字” -> 转换为区域文字

每个元素缩放,但是整体大小不变

Ctrl + Alt + Shift + D -> 分别变换

勾选:

  • 变换图案
  • 变换对象

SVG导入后会有蒙版,影响对各个元素的操作

释放蒙版->取消编组->删除蒙版

Share on

MiaoMiaoYang
WRITTEN BY
MiaoMiaoYang