为什么从ipython笔记本生成的matplotlib图与终端版本略有不同? [英] Why is matplotlib plot produced from ipython notebook slightly different from terminal version?

查看:93
本文介绍了为什么从ipython笔记本生成的matplotlib图与终端版本略有不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.使用IPython Notebook,我使用pandas和matplotlib创建了相当广泛的脚本来创建许多图表. 修补完成后,我将代码复制(并清理)到一个独立的python脚本中(这样我就可以将其压入svn中,并且我的论文共同作者也可以创建图表).

I have a strange issue. Using IPython Notebook, I created a quite extensive script using pandas and matplotlib to create a number of charts. When my tinkering was finished, I copied (and cleaned) the code into a standalone python script (so that I can push it into the svn and my paper co-authors can create the charts as well).

为方便起见,我将独立的python脚本再次导入到笔记本中并创建了许多图表:

For convenience, I import the standalone python script into the notebook again and create a number of charts:

import create_charts as cc
df = cc.read_csv_files("./data")
cc.chart_1(df, 'fig_chart1.pdf')
...

奇怪的是,使用上述方法获得的.pdf文件与从Windows 7终端运行独立的python脚本时获得的.pdf文件略有不同.最明显的区别是,在特定图表中,图例位于上角而不是下角.但是还有其他一些小的区别(边框大小,字体似乎略有不同)

Strange enough, the .pdf file I get using the above method is slightly different from the .pdf file I get when I run my standalone python script from my Windows 7 terminal. The most notable difference is that in a particular chart the legend is located in the upper corner instead of the lower corner. But there are other small diferences as well (bounding box size, font seems slightly different)

这可能是什么原因.而我该如何解决呢? (我已经关闭我的笔记本并重新启动它,以重新导入我的create_charts脚本并排除所有未保存的更改) 我的终端报告我正在使用Python 2.7.2,而pip freeze | grep ipython报告我正在使用Python 2.13.1

What could be the cause of this. And how can I troubleshoot it? (I already shut down my notebook and restarted it, to reimport my create_charts script and rule out any unsaved changes) My terminal reports I am using Python 2.7.2, and pip freeze | grep ipython reports ipython 0.13.1

推荐答案

扩展了Matt的答案(很多功劳,但我认为答案可能不太复杂),这就是我最终解决问题的方法.

Extending Matt's answer (lots of credit to him, but I think the answers can be less complex), this is how I eventually solved it.

(a)我在C:\Python27\Lib\site-packages\IPython\zmq\pylab\backend_inline.py中查找了ipython的默认matplotlib设置(请参阅Matt的答案).

(a) I looked up ipython's default matplotlib settings in C:\Python27\Lib\site-packages\IPython\zmq\pylab\backend_inline.py (see Matt's answer).

(b),并通过在脚本中插入以下代码,用终端版本中设置的值覆盖它们(我使用print mpl.rcParams['figure.figsize']等进行查找):

(b) and overwrote them with the values as set in the terminal version (I used print mpl.rcParams['figure.figsize'] etc. to find out) by inserting the following code in my script:

import matplotlib as mpl

#To make sure we have always the same matplotlib settings
#(the ones in comments are the ipython notebook settings)

mpl.rcParams['figure.figsize']=(8.0,6.0)    #(6.0,4.0)
mpl.rcParams['font.size']=12                #10 
mpl.rcParams['savefig.dpi']=100             #72 
mpl.rcParams['figure.subplot.bottom']=.1    #.125

这篇关于为什么从ipython笔记本生成的matplotlib图与终端版本略有不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆