首页 > 解决方案 > 如何编辑图形的大小?

问题描述

请问如何调整图表的大小?这是我的代码。

import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(mean, median, marker="s", linestyle="")
for i, txt in enumerate(words):
    ax.annotate(txt, (mean[i],median[i]))
ax.set_xlabel("median")
ax.set_ylabel("mean")

plt.show()

我试着用

fig,ax=plt.subplots(figsize=(20,10))

但失败了。

标签: pythonmatplotlib

解决方案


在调整图形大小之前,您首先必须拥有可以执行的代码:

(我添加了虚拟数据,现在它可以工作了)

import matplotlib.pyplot as plt

if __name__ == '__main__':

    fig = plt.figure()
    ax = fig.add_subplot(111)

    mean, median = [1, 2, 3], [4, 5, 6]   # dummy data

    ax.plot(mean, median, marker="s", linestyle="")

    for i, txt in enumerate(['a', 'b', 'c']):
        ax.annotate(txt, (mean[i], median[i]))

    ax.set_xlabel("median")
    ax.set_ylabel("mean")

    fig, ax = plt.subplots(figsize=(10, 10))   # size in inches

    plt.show()

推荐阅读