gpt4 book ai didi

python - Matplotlib 如何更改 matshow 的 figsize

转载 作者:太空狗 更新时间:2023-10-29 17:13:58 26 4
gpt4 key购买 nike

如何更改 matshow() 的 figsize在 jupyter 笔记本中?

例如这段代码改变图形大小

%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd

d = pd.DataFrame({'one' : [1, 2, 3, 4, 5],
'two' : [4, 3, 2, 1, 5]})
plt.figure(figsize=(10,5))
plt.plot(d.one, d.two)

但是下面的代码不起作用

%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd

d = pd.DataFrame({'one' : [1, 2, 3, 4, 5],
'two' : [4, 3, 2, 1, 5]})
plt.figure(figsize=(10,5))
plt.matshow(d.corr())

最佳答案

默认情况下,plt.matshow() 生成自己的图形,因此结合 plt.figure() 将创建两个图形,一个承载matshow 图不是设置了 figsize 的图。

有两种选择:

  1. 使用fignum参数

    plt.figure(figsize=(10,5))
    plt.matshow(d.corr(), fignum=1)
  2. 使用 matplotlib.axes.Axes.matshow 而不是 pyplot.matshow 绘制 matshow。

    fig, ax = plt.subplots(figsize=(10,5))
    ax.matshow(d.corr())

关于python - Matplotlib 如何更改 matshow 的 figsize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43021762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com