Matplotlib how to change figsize for matshow

Question:

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())
Asked By: chinskiy

||

Answers:

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

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.

Answered By: Elias Hasle

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')
Answered By: Haeraeus
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.