给matplotlib images添加scale Bar
Scale Bar(比例尺)用于直观表示图像与实际物理尺寸(如微米、毫米等)的对应关系。
例如,在显微镜图像中,比例尺可以标注“75μm”表示图中某线段对应的实际长度。
这里分享使用matplotlib中的imshow结合matplotlib-scalebar为imags plot添加Scale Bar。
首先,使用matplotlib imshow搞定imags plot,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib_scalebar.scalebar import ScaleBar
import matplotlib.image as mpimg
%config InlineBackend.figure_format='retina'
%config InlineBackend.print_figure_kwargs = {'pad_inches':0}
defimshow_bar(im, scalebar):
fig, ax = plt.subplots(figsize=(2, 2), dpi=60)
ax.axis("off")
ax.imshow(im)
ax.add_artist(scalebar)
img = mpimg.imread("green.png")
然后,使用matplotlib_scalebar中的ScaleBar添加比例尺。添加"cm"级比例尺,
scalebar = ScaleBar(0.3, "mm") #定义比例尺
imshow_bar(img, scalebar)
添加"mm"级比例尺,
scalebar = ScaleBar(0.3 / 5, "mm")#定义比例尺
imshow_bar(imgx5, scalebar)
添加"nm"级比例尺,
scalebar = ScaleBar(0.3 / 10000000, "mm")#定义比例尺
imshow_bar(imgx10000000, scalebar)
更多个性化比例尺,
参考:https://github.com/ppinard/matplotlib-scalebar
-END-
👉matplotlib教程:20w字+数百张图+1W行代码+详细代码注释+学习交流群
✅部分教程案例欣赏:
👇加入学习(备注:399),