猿问

为什么我在 matplotlib 中的图没有显示轴

我在处理绘图时遇到了问题,因为在我处理它时,轴标签似乎显示在 Jupyter Notebooks 中。


但是,当我将文件导出到 .py 文件并在终端中运行它时,给出的图表没有轴标签。


fig = plt.figure(figsize = (15,5))


ax = fig.add_axes([0,0,1,1])

ax.set_title('Oil vs Banks Mean Return')

ax.set_xlabel('Date')

ax.set_ylabel('Price')


ax.plot(all_returns['Mean'], label = 'Banks Mean', color = 'green')

ax.plot(all_returns['Oil'], label = 'Oil', color = 'black')

ax.plot(movavg['Mean'], label = 'Mean MA', color = 'blue')

ax.plot(movavg['Oil'], label = 'OIL MA', color = 'red')


ax.legend()


plt.tight_layout();

在 Jupyter Notebooks 中,它显示轴和标签,例如。年份等:

但是,当我导出它时,它们不见了:

http://img.mukewang.com/60c8699900010cbb14360895.jpg

达令说
浏览 883回答 2
2回答

DIEA

线ax = fig.add_axes([0,0,1,1])导致问题。在这里,您告诉matplotlib将所有图形空间用于实际绘图,而不为轴和标签保留任何空间。tight_layout()如果Axes以这种方式创建实例,则似乎没有任何影响。相反,用ax = fig.add_subplot(111)你应该很高兴去。

守候你守候我

试试这个选项:color_name = "grey"ax.spines["top"].set_color(color_name)ax.spines["bottom"].set_color(color_name)ax.spines["left"].set_color(color_name)ax.spines["right"].set_color(color_name)
随时随地看视频慕课网APP

相关分类

Python
我要回答