org-mode babel 和 matplotlib 中绘图背后的阴影
Shading behind the plot in org-mode babel and matplotlib
考虑以下 python org-mode 中带有 babel 的代码块:
#+begin_src python :results none
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
graylevel = 0.75
mpl.rc('figure', facecolor = (graylevel, graylevel, graylevel), edgecolor ='r')
X = np.linspace(0, 7, 1024)
plt.plot(X, np.sin(X))
plt.plot(X, np.cos(X))
plt.draw()
plt.show()
#+end_src]
在弹出窗口 window 中,这会生成灰色背景图
但是,当将绘图放入文件中时,还是在组织模式下(只有最后几行不同)
#+begin_src python :results file
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
graylevel = 0.75
mpl.rc('figure', facecolor = (graylevel, graylevel, graylevel), edgecolor ='r')
X = np.linspace(0, 7, 1024)
plt.plot(X, np.sin(X))
plt.plot(X, np.cos(X))
plt.draw()
plt.savefig('test.png')
return 'test.png'
#+end_src
#+RESULTS:
[[file:test.png]]
灰色背景消失了
我确实需要灰色背景来从 org-mode 导出我的图表,因为我需要图表从周围的文档中脱颖而出。
我不知道这是 matplotlib、org-mode、python、我的机器还是我自己的问题。我希望有人可以重现这个例子或者也许只是知道答案。
我认为这不是 org-mode 的问题,而是同一个问题 discussed here。
这是解决您的问题的方法:
plt.savefig('test.png', facecolor=(graylevel, graylevel, graylevel))
考虑以下 python org-mode 中带有 babel 的代码块:
#+begin_src python :results none
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
graylevel = 0.75
mpl.rc('figure', facecolor = (graylevel, graylevel, graylevel), edgecolor ='r')
X = np.linspace(0, 7, 1024)
plt.plot(X, np.sin(X))
plt.plot(X, np.cos(X))
plt.draw()
plt.show()
#+end_src]
在弹出窗口 window 中,这会生成灰色背景图
但是,当将绘图放入文件中时,还是在组织模式下(只有最后几行不同)
#+begin_src python :results file
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
graylevel = 0.75
mpl.rc('figure', facecolor = (graylevel, graylevel, graylevel), edgecolor ='r')
X = np.linspace(0, 7, 1024)
plt.plot(X, np.sin(X))
plt.plot(X, np.cos(X))
plt.draw()
plt.savefig('test.png')
return 'test.png'
#+end_src
#+RESULTS:
[[file:test.png]]
灰色背景消失了
我确实需要灰色背景来从 org-mode 导出我的图表,因为我需要图表从周围的文档中脱颖而出。
我不知道这是 matplotlib、org-mode、python、我的机器还是我自己的问题。我希望有人可以重现这个例子或者也许只是知道答案。
我认为这不是 org-mode 的问题,而是同一个问题 discussed here。
这是解决您的问题的方法:
plt.savefig('test.png', facecolor=(graylevel, graylevel, graylevel))