【问题标题】:How do I increase Bar Chart size with Matplotlib [duplicate]如何使用 Matplotlib 增加条形图大小 [重复]
【发布时间】:2021-08-04 21:57:06
【问题描述】:

我无法增加条形图的大小。我正在使用模板,但不知道如何增加尺寸。目前我的条形图和标签如图所示重叠。

关于如何解决的任何想法?

模板代码:

import matplotlib.pyplot as plt
import numpy as np


labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()

plt.show()

【问题讨论】:

  • fig, ax = plt.subplots(figsize=...).
  • 当您设置subplots 时,您可以传递figsize=(width, height)。见docs

标签: python python-3.x numpy matplotlib


【解决方案1】:

你可以使用 figsize() 方法:

plt.figure(figsize=(width, height)) 

因此,根据您的 sn-p 有用的代码将是 -

fig, ax = plt.subplots(figsize=(width, height))

【讨论】:

  • 请不要使用plt.figure(),因为OP已经使用了fig, ax = plt.subplots()plt.figure() 将创建一个新的空图。
  • 感谢指正。我也编辑了我的答案。
猜你喜欢
  • 2017-10-04
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
相关资源
最近更新 更多