问题描述
我有一个奇怪的问题.使用 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
报告 ipython 0.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:Python27Libsite-packagesIPythonzmqpylabackend_inline.py
中查找了 ipython 的默认 matplotlib 设置(参见 Matt 的回答).
(a) I looked up ipython's default matplotlib settings in C:Python27Libsite-packagesIPythonzmqpylabackend_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 notebook 生成的 matplotlib 图与终端版本略有不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!