pandas Matplotlib 如何更改 matshow 的 figsize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43021762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:16:42  来源:igfitidea点击:

Matplotlib how to change figsize for matshow

pythonpandasmatplotlib

提问by chinskiy

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

如何在 jupyter notebook 中更改 matshow ()的 figsize?

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

回答by ImportanceOfBeingErnest

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.

默认情况下,plt.matshow()生成自己的图形,因此plt.figure()将创建两个图形的组合,并且承载 matshow 图的图形不是具有 figsize 集的图形。

There are two options:

有两种选择:

  1. Use the fignumargument

    plt.figure(figsize=(10,5))
    plt.matshow(d.corr(), fignum=1)
    
  2. Plot the matshow using matplotlib.axes.Axes.matshowinstead of pyplot.matshow.

    fig, ax = plt.subplots(figsize=(10,5))
    ax.matshow(d.corr())
    
  1. 使用fignum参数

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

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

回答by Elias Hasle

Improving on the solution by @ImportanceOfBeingErnest,

通过@ImportanceOfBeingErnest 改进解决方案,

matfig = plt.figure(figsize=(8,8))
plt.matshow(d.corr(), fignum=matfig.number)

This way you don't need to keep track of figure numbers.

这样你就不需要跟踪数字。

回答by Haeraeus

The solutions did not work for me but I found another way:

这些解决方案对我不起作用,但我找到了另一种方法:

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