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())
First off, the easiest way to change the size of a figure is to use the figsize argument. You can use this argument either in Pyplot's initialization or on an existing Figure object. Here, we've explicitly assigned the return value of the figure() function to a Figure object.
Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.
Figure size, aspect ratio and DPI Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created, using the figsize and dpi keyword arguments. figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch).
figsize() takes two parameters- width and height (in inches). By default the values for width and height are 6.4 and 4.8 respectively. Where, x and y are width and height respectively in inches.
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())
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With