Matplotlib savefig does not save axes

前端 未结 2 1413
说谎
说谎 2021-01-13 09:39

I\'m trying to save a figure that works fine in IPython inline but does not save the figure to disk with the axes and titles included.

I am using TKAgg backend by de

相关标签:
2条回答
  • 2021-01-13 10:18

    I was having the same problem using Jupyter notebook and the command: %matplotlib notebook. The figure showed correctly in the notebook but didn't print axis and titles when saved with fig.savefig(). I changed %matplotlib notebook to %matplotlib inline and that solved the problem.

    0 讨论(0)
  • 2021-01-13 10:31

    You are setting the axis to start at the very bottom left of the figure and to fill up the entire thing. There's no room for the axis labels or the title. Try this:

    import matplotlib.pylab as plt  
    x = [1,2,3,3]
    y = map(lambda(x): x * 2, x)
    fig = plt.figure()
    ax = fig.add_axes([0.1,0.1,0.75,0.75]) # axis starts at 0.1, 0.1
    ax.set_title("bleh")
    ax.set_xlabel("xlabel")
    ax.plot(x, y, 'r--')
    fig.savefig("fig.png")
    

    plot with axis showing

    0 讨论(0)
提交回复
热议问题