chinskiy
chinskiy

Reputation: 2715

Matplotlib how to change figsize for 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())

Upvotes: 29

Views: 27951

Answers (3)

Haeraeus
Haeraeus

Reputation: 161

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

Upvotes: 15

Elias Hasle
Elias Hasle

Reputation: 667

Improving on the solution by @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.

Upvotes: 10

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

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.

There are two options:

  1. Use the fignum argument

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

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

Upvotes: 49

Related Questions