pyplot.subplots:python 和 jupyter notebook 中的不同行为 [英] pyplot.subplots: different behavior in python and jupyter notebook
问题描述
在参加 kaggle 比赛时,我遇到了一些奇怪的问题.基本上,我试图将图像的矢量表示转换为 png 文件.它在 iPython 中完美运行,代码如下:
While taking part in kaggle competition, i got some weird problem. Basically, I am trying to convert vector representation of am image to png file. It worked perfectly in iPython, code below:
def drawing_to_np_prepare_data(drawing):
drawing = eval(drawing)
fig, ax = plt.subplots()
plt.close(fig)
print('[debug] ax=',ax)
for x,y in drawing:
ax.plot(x, y, marker='.')
ax.axis('off')
fig.canvas.draw()
# Convert images to numpy array
np_drawing = np.array(fig.canvas.renderer._renderer)
print('[debug] fig_size=',fig.get_size_inches())
print('[debug] dpi=',fig.dpi)
print('[debug] shape=',np_drawing.shape)
print('[debug] size=',np_drawing.size)
print('[debug] shape=',np_drawing.shape)
im = cv2.cvtColor(np_drawing.astype(np.uint8), cv2.COLOR_BGR2RGB)
# compress
compressed_array = io.BytesIO()
np.savez_compressed(compressed_array, im)
compressed_array.seek(0)
print('[debug] size=',np_drawing.shape)
return compressed_array
结果显示:
[debug] ax=AxesSubplot(0.125,0.125;0.775x0.755)
[debug] fig_size= [6. 4.]
[debug] dpi= 72.0
[debug] np_drawing.size= 497664
[debug] shape= (288, 432, 4)
[debug] size= 1880
可以满足我的需求:我得到的图像压缩后的尺寸<2Kb
which satisfy my needs: i am getting an image with compressed size < 2Kb
然而,当我从 CLI 在 python 中运行这段代码时,我得到了完全不同的结果:
However, when I run this code in python from CLI, I am getting quite different result:
[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)
[debug] fig_size= [6.4 4.8]
[debug] dpi= 100.0
[debug] np_drawing.size= 1228800
[debug] shape= (480, 640, 4)
[debug] size= 13096
如您所见,图形大小、dpi、轴不同,因此最后的大小也不同.
as you can see, figure size, dpi, axes are different and as a result, size at the end are also different.
我可以将参数传递给子图:
I can pass arguments to subplots:
plt.subplots(figsize=(6.,4.), dpi=72)
可校正除轴以外的参数(由于轴的不同,我猜是和尺寸):
which corrects parameters except axes (and size, I guess because of different axes):
[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)
[debug] fig_size= [6. 4.]
[debug] dpi= 72.0
[debug] np_drawing.size= 497664
[debug] shape= (288, 432, 4)
[debug] size= 8214
注意:我已经检查了库的版本,它们是相同的.
Note: I've checked library versions and they are the same.
因此,出现了多个问题:
So, multiple questions arise:
-
为什么子图会给出不同的轴,形状和分辨率?
Why subplots give different axes, shape and resolution?
如何修正坐标轴?
如何在python中获得相同的行为?
How to get the same behaviors in python?
我想了解发生了什么.谢谢!
I want to understand the what is going on. Thanks!
推荐答案
要在脚本中获得与笔记本中完全相同的设置,请打开笔记本,运行
To get the exact same settings in a script as in your notebook, open a notebook, run
%matplotlib inline
%config InlineBackend.rc
它将打印一个 rcParams 字典.
It'll print a dictionary of rcParams.
{'figure.figsize': (6.0, 4.0),
'figure.facecolor': (1, 1, 1, 0),
'figure.edgecolor': (1, 1, 1, 0),
'font.size': 10,
'figure.dpi': 72,
'figure.subplot.bottom': 0.125}
将这些内容复制为您的python文件
Copy those to your python file as
newrc = {'figure.figsize': (6.0, 4.0),
'figure.facecolor': (1, 1, 1, 0),
'figure.edgecolor': (1, 1, 1, 0),
'font.size': 10,
'figure.dpi': 72,
'figure.subplot.bottom': 0.125}
import matplotlib.pyplot as plt
plt.rcParams.update(newrc)
然后做你的情节.
这是否真的解决了不同渲染器尺寸的问题无法测试,因为该问题不包含可运行示例.
Whether or not this actually solves the problem of different renderer sizes cannot be tested because the question does not contain a runnable example.
这篇关于pyplot.subplots:python 和 jupyter notebook 中的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!