Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib how to change figsize for matshow

Tags:

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()) 
like image 592
chinskiy Avatar asked Mar 25 '17 20:03

chinskiy


People also ask

How do you change Figsize?

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.

How do I increase Figsize in Matplotlib?

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.

What is Figsize Pyplot?

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

How do you use Figsize?

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.


2 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()) 
like image 109
ImportanceOfBeingErnest Avatar answered Nov 07 '22 05:11

ImportanceOfBeingErnest


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') 
like image 33
Haeraeus Avatar answered Nov 07 '22 05:11

Haeraeus