如何在缩放后保存图表

0 投票
1 回答
1771 浏览
提问于 2025-04-18 13:31

最近几天我遇到了一个问题,关于保存matplotlib图形。三天前我的代码运行得很好,可以保存经过代码修改后的图表,但现在保存的图表没有反映出我所做的更改(比如缩放后的图表),而是保存了显示时的样子,之后做的任何更改都没有通过保存命令反映出来,我也不知道为什么会这样。

    ax = pd.rolling_mean(dataToPlot_plot[startTime:endTime][['plotValue']],mar).plot(linestyle='-', linewidth=3,  markersize=9, color='#FECB00')
            ax.legend().set_visible(False)
    plt.show()#showing the plot
    fig = ax.get_figure()
    fig.set_size_inches(12, 6)#setting the size of the plot, to fix in the PDF file
    fig.savefig('graph1.jpg')#saving the plot

即使我调用了一个函数,新的更改后的图表也没有被保存……

    def callmeto_plot()
            ax = pd.rolling_mean(dataToPlot_plot[startTime:endTime][['plotValue']],mar).plot(linestyle='-', linewidth=3,  markersize=9, color='#FECB00')
            ax.legend().set_visible(False)
            plt.show()#showing the plot
            fig = ax.get_figure()

            return fig

    fig = callmeto_plot()
    fig.set_size_inches(12, 6)
    fig.savefig('graph1.jpg')

我该如何通过代码保存这个(带有更改的缩放图)图表呢?
注意:我发现图表窗口的外观也发生了变化,
1 之前的图表窗口外观
2 现在的图表窗口外观
所有的图表窗口配置按钮都从底部移到了顶部,这个变化只影响图表,还是也影响了代码呢?请帮我解决这个问题……
先谢谢你了。

1 个回答

0

你可以把所有改变图表的代码放在 plt.show() 之前,然后从弹出的窗口手动保存这个图。左下角最右边的图标可以把当前显示的图保存下来。

更好的方法是使用 plt.axis([xStart, xFinish, yBottom, yTop]) 或者 ax.set_xlim(), ax.set_ylim 来设置坐标轴的范围。

撰写回答