Python matplotlib以像素为单位使用规格(大小、对齐等)渲染图像

Python matplotlib以像素为单位使用规格(大小、对齐等)渲染图像,python,matplotlib,Python,Matplotlib,我有一个模型的规格,例如,Photoshop/Illustrator/Sketch文件,每个元素都有规格,比如大小、位置和像素坐标 图像如下所示: 我可以使用标准matplotlib技术绘制类似的图像,没有任何问题 问题是,如何使用规格准确地渲染图像?所有大小、字体大小、对齐方式应与规格中的相同(如Illustrator中所示) 我研究过matplotlib文档,但即使是教程也没有帮助 用一个确切的例子更新 我在Zeplin有一个模拟图,显示了每个图的坐标(图中也是一个图)。所以我知道,图像的

我有一个模型的规格,例如,Photoshop/Illustrator/Sketch文件,每个元素都有规格,比如大小、位置和像素坐标

图像如下所示:

我可以使用标准matplotlib技术绘制类似的图像,没有任何问题

问题是,如何使用规格准确地渲染图像?所有大小、字体大小、对齐方式应与规格中的相同(如Illustrator中所示)

我研究过matplotlib文档,但即使是教程也没有帮助

用一个确切的例子更新

我在Zeplin有一个模拟图,显示了每个图的坐标(图中也是一个图)。所以我知道,图像的边距是25x25px,它的大小是80x80像素

(同样不允许嵌入图像)

你会怎么做

我用于绘图的代码

fig, ax = plt.subplots(1, 2, figsize=(20, 10), sharey=True)

recs = ax[0].barh(y_pos, widths, align='edge');

img = mpimg.imread('/Users/iwitaly/Downloads/2017-11-12 17.40.46.jpg')

ax[0].spines['left'].set_visible(False);
ax[0].spines['right'].set_visible(False);
ax[0].spines['bottom'].set_visible(False);
ax[0].spines['top'].set_visible(False);

ax[0].get_xaxis().set_visible(True);
ax[0].get_yaxis().set_visible(True);


obj = ax[0].text(x=0, y=3.9, s=r'Name: Vitaly Davydov',
          fontsize=30, fontname="Courier New", weight='bold');

ax[0].axhline(y=3.85, xmin=0, xmax=0.75, color='black');

# I'd like to place axicon exactly with 25x25 marging from top left corner
axicon = fig.add_axes([0.08, 1, 0.2, 0.2], transform=None)
axicon.axis('off');
axicon.imshow(img, interpolation='none');


for i, r in enumerate(recs):
    r.set_color(index_to_color[i]);
    r.set_height(col_wight);
    ax[0].text(x=0, y=text_positions[i], s=index_to_text[i], fontsize=30, fontname="Courier New");


ax[1].spines['left'].set_visible(False);    
ax[1].spines['right'].set_visible(False);
ax[1].spines['bottom'].set_visible(False);
ax[1].spines['top'].set_visible(False);

ax[1].get_xaxis().set_visible(False);
ax[1].get_yaxis().set_visible(False);


ax[1].text(x=0, y=3.9, s='Increment:', fontsize=30,
           fontname="Courier New", weight='bold');

ax[1].axhline(y=3.85, xmin=0, xmax=0.4, color='black');

for i, r in enumerate(recs):
    text_x, text_y = r.xy

    increment_pos_y = text_y + col_wight / 2
    if increment_values[i] > 0:        
        increment_text = '+{}'.format(increment_values[i])
    elif increment_values[i] < 0:
        increment_text = '-{}'.format(increment_values[i])
    else:
        increment_text = '{}'.format(increment_values[i])


    ax[1].text(x=0, y=increment_pos_y, s=increment_text,
               fontsize=30, color=index_to_color[i],
               fontname="Courier New", weight='bold');  
fig,ax=plt.子图(1,2,figsize=(20,10),sharey=True)
recs=ax[0]。barh(y_pos,widths,align='edge');
img=mpimg.imread('/Users/iwitaly/Downloads/2017-11-12 17.40.46.jpg'))
ax[0]。脊椎['left']。设置_可见(False);
ax[0]。脊椎['right']。设置_可见(False);
ax[0]。脊椎['bottom']。设置_可见(False);
ax[0]。脊椎['top']。设置为可见(False);
ax[0].get_xaxis().set_visible(True);
ax[0]。get_yaxis()。set_visible(True);
obj=ax[0]。文本(x=0,y=3.9,s=r'Name:Vitaly Davydov',
fontsize=30,fontname=“Courier New”,weight='bold');
ax[0].axhline(y=3.85,xmin=0,xmax=0.75,color='black');
#我想从左上角精确放置带有25x25边缘的axicon
轴棱锥=图。添加轴([0.08,1,0.2,0.2],变换=无)
轴棱锥轴(“关闭”);
axicon.imshow(img,interpolation='none');
对于枚举中的i,r(REC):
r、 设置颜色(索引为颜色[i]);
r、 设置高度(柱高);
ax[0]。text(x=0,y=text_位置[i],s=index_to_text[i],fontsize=30,fontname=“Courier New”);
ax[1]。脊椎['left']。设置_可见(False);
ax[1]。脊椎['right'].set_可见(False);
ax[1]。脊椎['bottom'].set_可见(False);
ax[1]。脊椎['top']。设置为可见(False);
ax[1]。get_xaxis()。set_visible(False);
ax[1]。get_yaxis()。set_visible(False);
ax[1]。文本(x=0,y=3.9,s='Increment:',fontsize=30,
fontname=“Courier New”,weight='bold');
ax[1].axhline(y=3.85,xmin=0,xmax=0.4,color='black');
对于枚举中的i,r(REC):
text_x,text_y=r.xy
增量位置y=文本y+列宽度/2
如果增量_值[i]>0:
增量_文本='+{}'。格式(增量_值[i])
elif增量_值[i]<0:
增量_文本='-{}'。格式(增量_值[i])
其他:
增量_文本='{}'。格式(增量_值[i])
ax[1]。文本(x=0,y=increment\u pos\u y,s=increment\u text,
fontsize=30,color=index\u to\u color[i],
fontname=“Courier New”,weight='bold');

在本例中,我想放置axicon轴,该轴是一个边距为25x25、大小为80x80(全部以像素为单位)的图像。

要将轴放置到图形中,您可以使用
fig.add_轴([left,bottom,width,height])
,其中
left,bottom width,height
是图形大小的分数

要将像素转换为图形大小的分数,需要将像素除以图形dpi和图形大小。例如,对于左边缘

left = 25/fig.dpi/fig.get_size_inches()[0]
完整示例:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1, 2, figsize=(10, 7))
y_pos, widths = np.linspace(0,3,4), np.random.rand(4)
recs = ax[0].barh(y_pos, widths, align='edge');

#img = plt.imread('/Users/iwitaly/Downloads/2017-11-12 17.40.46.jpg')
img = np.random.rand(80,80)

# I'd like to place axicon exactly with 25x25 marging from top left corner
x,y= 25,25 #pixels
dx,dy = 80,80
w,h = fig.get_size_inches()
axicon = fig.add_axes([x/float(fig.dpi)/w, 1.-(y+dy)/float(fig.dpi)/h, 
                       dx/float(fig.dpi)/w, dy/float(fig.dpi)/h])
axicon.axis('off');
axicon.imshow(img, interpolation='none');

plt.show()

使用matplotlib确实可以做到这一点。本教程确实是一个开始。对于在这里提出的问题,您可能希望给出一个示例,包括您尝试过的代码,并清楚地说明您在哪一点上遇到了问题。否则这太宽泛了。@importanceofbeingerest谢谢!我已经添加了一个代码,我如何用精确的问题绘制图像。谢谢!那帮了大忙!