Matplotlib 如何更改 matshow 的 figsize [英] Matplotlib how to change figsize for matshow

查看:35
本文介绍了Matplotlib 如何更改 matshow 的 figsize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to change figsize for matshow() in jupyter notebook?

例如此代码更改图形大小

For example this code change figure size

%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)

但是下面的代码不起作用

But code below doesn't work

%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()<结合使用/code> 将创建两个图形,承载 matshow 图的图形不是设置了 figsize 的图形.

By default, plt.matshow() produces its own figure, so in combination with plt.figure() two figures will be created and the one that hosts the matshow plot is not the one that has the figsize set.

有两种选择:

  1. 使用 fignum 参数

plt.figure(figsize=(10,5))
plt.matshow(d.corr(), fignum=1)

  • 使用 matplotlib.axes.Axes.matshow 而不是 pyplot.matshow 绘制Matshow.

  • Plot the matshow using matplotlib.axes.Axes.matshow instead of pyplot.matshow.

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

  • 这篇关于Matplotlib 如何更改 matshow 的 figsize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆