简体   繁体   English

如何使用matplotlib绘制特定像素大小的图形?

[英]How to draw a figure in specific pixel size with matplotlib?

Right now I'm working on a graphic project which requires me to draw a image that has specific element size in micrometer. 现在,我正在进行一个图形项目,该项目需要我绘制具有特定尺寸(以微米为单位)的图像。 And I'm using matplotlib to finish this task. 我正在使用matplotlib完成此任务。 However, I meet some problems in size control. 但是,我在尺寸控制方面遇到了一些问题。

I've calculated all the plot data to describe the image. 我已经计算了所有绘图数据以描述图像。 It basically is a grid with scatters on grid's cross line and there are hundreds of columns and rows in this grid and each axis for each cross line has its own width in micrometer as its size unit. 它基本上是一个在网格的交叉线上分散的网格,该网格中有成百上千的行和列,每个交叉线的每个轴都有自己的宽度(以微米为单位)作为尺寸单位。 Example image is just like this: 示例图片如下所示: 例

I've tried many ways to control element or artist size. 我尝试了许多方法来控制元素或艺术家的大小。 For making one pixel equals to one micrometer, I set a large DPI to the figure 为了使一个像素等于一微米,我将数字设置为较大的DPI

DPI = 25400
plt.figure(dpi=DPI)
fig = plt.gcf()
size = (
    width / DPI,
    height / DPI
)
fig.set_size_inches(size)

this can ensure that one pixel is one micrometer. 这样可以确保一个像素等于一微米。 However, I don't know how to control scatter and line width in pixel size. 但是,我不知道如何控制像素大小的散布和线宽。 the default s in plt.scatter or linewidth in plt.plot can't help to control the size. 默认的splt.scatterlinewidthplt.plot不禁的大小来控制。

I want to know how to control it in micrometers with matplotlib or should I just change another tool to draw this graph or even any other tools to meet this requirement? 我想知道如何使用matplotlib在千分尺上进行控制,还是应该仅更改另一个工具来绘制该图,甚至是其他工具来满足此要求?

Matplotlib's linewidth is in points; Matplotlib的线宽以磅为单位; scatter size is in points^2. 散点大小以点为单位^ 2。 For this see pyplot scatter plot marker size . 为此,请参见pyplot散点图标记大小

1 point == fig.dpi/72. pixels

Hence, 因此,

1 pixel == 72/fig.dpi points

If one micron is one pixel, and you want something of a size x microns, 如果1微米等于1像素,而您想要的尺寸是x微米,

x microns = x pixel = x*72 /fig.dpi points

hence 于是

plt.plot(..., linewidth=x * 72 / fig.dpi)
plt.scatter(..., s=(x * 72 / fig.dpi)**2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM