Matplotlib窗口大小、标题等

5

我刚开始学习Python和Matplotlib。我有一个项目,需要在Tkinter GUI中使用Matplotlib创建一张图形。我找到了并使用了以下代码:

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
menStd =   (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)
womenStd =   (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)


ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

但我找不到修改窗口大小的方法,即使我已经尝试使用以下代码:

figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

但这只是创建了另一个窗口!请帮忙!
1个回答

12

只需要替换

fig, ax = plt.subplots()

by:

fig, ax = plt.subplots(num=None, figsize=(16, 12), dpi=80, facecolor='w', edgecolor='k')
## I changed the fig size to something obviously big to make sure it worked

要更改图形窗口的标题,您可以使用:

fig.canvas.set_window_title('Test')

这对我很有帮助!你知道如何更改窗口标题和图标吗? - Cesar Borge Mora
@CesarBorgeMora 我编辑了我的答案,告诉你如何更改窗口标题。至于图标,我不确定,但可以查看这个问题:https://dev59.com/5WLVa4cB1Zd3GeqPzsg_。如果觉得有帮助,请接受我的答案 :-) - Julien Spronck
3
对我来说,只有在我在canvas后添加manager属性之后,上面的代码才能正常工作:fig.canvas.manager.set_window_title('Test') - René Schwaiger
@RenéSchwaiger 是的,它不作为答案,而是作为你的评论。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接