AlvinL
AlvinL

Reputation: 448

Figure dimensions, matplotlib, python3.4

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

def fig1():
    x = np.arange(-5,5,0.01)
    f1 = lambda x: x**2 +x -6
    f2 = lambda x: x*0
    plt.plot(x, f1(x))
    plt.plot(x, f2(x),'black')
    axes = plt.gca()
    axes.set_ylim([-10,10])
    axes.set_xlim([-5,5])
    plt.show()    

enter image description here How can I force this figure into specific dimensions? At the moment it is displayed on a rectangular plane, but I want to force the dimensions to be strictly n x n.

Upvotes: 2

Views: 1482

Answers (1)

Srivatsan
Srivatsan

Reputation: 9363

If you want to force a pyplot to a manually specified size, it can be done so by using the figsize parameter from the matplotlib.figure module

The example of two sizes, one rectangle and one square of dimensions 5x5 and 5x8 here in the below example.

import numpy as np
import matplotlib.pyplot as plt

def fig1():
    x = np.arange(-5,5,0.01)
    f1 = lambda x: x**2 +x -6
    f2 = lambda x: x*0
    fig = plt.figure(figsize=(5,8)) # IT IS HERE THAT WE SPECIFY THE FIGSIZE
    ax = fig.add_subplot(111)
    ax.plot(x, f1(x))
    ax.plot(x, f2(x),'black')
    ax.set_ylim([-10,10])
    ax.set_xlim([-5,5])
    plt.show()

when we use 5x5 we get something like this: enter image description here

and when we use 5x8 we get something like this:

enter image description here

Upvotes: 4

Related Questions