python - 如何创建一个填满屏幕的图形?

1 投票
2 回答
1388 浏览
提问于 2025-04-18 04:39

我想在整个屏幕上显示一个图形,或者至少希望图形窗口里没有“空白区域”。现在我使用 plt.tight_layout() 来减少一些空白,但还是没有完全去掉。理想情况下,我希望能够最大化这个图形,并去掉所有的坐标轴刻度。

import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.tight_layout() # <-- probably need something better here
plt.get_current_fig_manager()
mng.full_screen_toggle()

2 个回答

0

试试这个:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), range(10))

# hide axis:
[sp.set_visible(False) for sp in [ax.spines['top'], ax.spines['right'], ax.spines['left'], ax.spines['bottom']]]
ax.yaxis.tick_left()
ax.xaxis.tick_bottom()
ax.get_xaxis().set_ticks([])
ax.get_yaxis().set_ticks([])

# 0 pad:
plt.tight_layout(0)

# save fig:
plt.savefig("1.png")
0

这样做可以去掉空白和坐标轴。不过,我不太确定怎么让它全屏。你试过 mng.full_screen_toggle() 这个方法吗?它没有起作用吗?

import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
ax = plt.gca()

# remove white back ground
ax.set_frame_on(False)

# remove axis and ticks
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# No padding 
plt.tight_layout(pad=0)

# Make full screen
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

撰写回答