我一直在尝试保存用matplotlib制作的图,但遇到了一些问题:不仅遇到了常见的白色边距问题(我在网上找到了一些解决方案),而且看来我的坐标轴和标签都在当我保存图像时,它们消失了,尽管当我要求Python show()
结果时它们看起来很好。这是MWE,是我用show()
得到的结果的打印屏幕(这是我想要的结果),以及将图形保存到.png时得到的结果(我相信白色边距确实很可靠,因为当我测试以相同方式生成的.svg文件时它们并不透明)。
from pylab import *
import numpy as np
L=5.05
dx=0.01
def func(x,v):
return np.cos(2*np.pi*v*x)*np.exp(-np.pi*x**2)
def main():
fig, ax = plt.subplots(1, 1,figsize = (12,8))
#fig.subplots_adjust(hspace=0)
ax.set_facecolor((0.118, 0.118, 0.118))
fig.patch.set_facecolor((0.118, 0.118, 0.118))
ax.grid(linewidth='0.25')
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('white')
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.annotate(r'$\nu=2$', xy=(5.1, -0.9), color='white')
(c2,) = ax.plot(np.arange(-L, L, dx), func(np.arange(-L, L, dx),2), color=(0.949,0.506,0.396), linewidth = 1.8)
ax.set_xlabel(r"Tempo $t$", color='white')
show()
return
Printscreen of the desired result (obtained with show()
)
Result obtained when saving figure through GUI
有什么想法吗?
参考方案
保存图形时,在图形周围区域中会绘制由fig.patch.set_facecolor((0.118, 0.118, 0.118))
设置的背景。标签仍然在那里,它们只是不可见的,因为它们是白色的。
将您的输出与没有设置以下背景色的同一图进行比较。
如果将facecolor
参数传递给.savefig
,它将在整个图像后面绘制此颜色,并且标签将按预期显示。
fig.savefig('testoutput.png', facecolor=(0.118, 0.118, 0.118))
Matplotlib'粗体'字体 - python跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…
Matplotlib-固定x轴缩放比例和自动缩放y轴 - python我只想绘制部分数组,固定x部分,但让y部分自动缩放。我尝试如下所示,但是它不起作用。有什么建议么?import numpy as np import matplotlib.pyplot as plt data=[np.arange(0,101,1),300-0.1*np.arange(0,101,1)] plt.figure() plt.scatter(da…
如何在Matplotlib条形图后面绘制网格线 - pythonx = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=…
Matplotlib表行标签字体的颜色和大小 - python给出下表:import matplotlib.pyplot as plt table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values rowLabels=['1&…
如何创建代表每天多个时间间隔的图形 - python考虑到以下dict包含每天的开放/关闭时间对:timetable = { 'monday': ['08:00', '12:00', '13:00', '18:00'], 'tuesday': ['08:00', …