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())
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:
-
Use the fignum
argument
plt.figure(figsize=(10,5))
plt.matshow(d.corr(), fignum=1)
-
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.
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')
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())
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:
-
Use the
fignum
argumentplt.figure(figsize=(10,5)) plt.matshow(d.corr(), fignum=1)
-
Plot the matshow using
matplotlib.axes.Axes.matshow
instead ofpyplot.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.
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')