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

标签 python pandas 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 中,它显示轴和标签,例如。年份等:
In Jupyter Notebooks it shows the axes and labels eg. Year etc.

但是,当我导出它时,它们不见了:
However, when I export it, they are gone

最佳答案

线

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/

相关文章:

python - 如何在matplotlib中的子图下方添加图例?

Python:对象作为另一个对象的参数

python - ImportError : No module named flask. ext.reSTLess

python - 如何添加一列 timedelta 并将其与另一列关联?

python - 如何在 pandas unstack 之前动态重命名列?

python - 如何在 python 中使用 Matplotlib 可视化 SVM?

python - 使用 python 删除列表中的重复条目

Python len() 不返回整数

python - Pandas:删除包含整数和字符串的混合类型系列中的所有字符串组件

Matplotlib:确保完整的破折号模式出现在图例中