簡體   English   中英

為什么我在 matplotlib 中的繪圖沒有顯示坐標軸

[英]Why are my plots in matplotlib not showing the axes

我在繪制繪圖時遇到了問題,因為在我處理它時,軸標簽似乎顯示在 Jupyter Notebooks 中。

但是,當我將文件導出到 a.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 筆記本中,它顯示軸和標簽,例如。 年份等: 在 Jupyter 筆記本中,它顯示軸和標簽,例如。年份等

但是,當我導出它時,它們消失了: 但是,當我導出它時,它們消失了

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

導致問題。 在這里,您告訴matplotlib將所有圖形空間用於實際繪圖,而不為軸和標簽保留任何空間。 如果以這種方式創建Axes實例, tight_layout()似乎沒有效果。 相反,用

ax = fig.add_subplot(111)

你應該很高興去。

我有同樣的問題,我將來可能也會為我留下這個解決方案。

問題出在子圖配置工具子圖配置工具

所以,我只需要給顯示器更多的空間

plt.subplots_adjust(left=0.2,bottom=0.2, top = 0.9, right = 0.9)

試試這個選項:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM