我在处理绘图时遇到了问题,因为在我处理它时,轴标签似乎显示在 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 中,它显示轴和标签,例如。年份等:
但是,当我导出它时,它们不见了:
最佳答案
线
ax = fig.add_axes([0,0,1,1])
导致问题。在这里告诉你
matplotlib
将所有图形空间用于实际绘图,而不为轴和标签留下任何空间。 tight_layout()
如果 Axes
似乎没有效果实例就是这样创建的。相反,用ax = fig.add_subplot(111)
你应该很高兴去。
关于python - 为什么我在 matplotlib 中的绘图没有显示轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272393/