Home > Mobile >  Remove white space plot matplotlib
Remove white space plot matplotlib

Time:11-19

I'm trying to get something like this:

enter image description here

with this code

x = np.arange(l, r, s)
y = np.arange(b, t, s)
X, Y = np.meshgrid(x, y)
Z = f(X,Y)

plt.axis('equal')
plt.pcolormesh(X, Y, Z)
plt.savefig("image.png",dpi=300)

But I get this:

enter image description here

How could I remove the white regions? I really appreciate any kind of help.

CodePudding user response:

i would use the pyplot subplots to define the figures size and therefor aspect like this

import numpy as np
from matplotlib import pyplot as plt

def f(x,y):
    return x   y

x = np.arange(1, 10, .1)
y = np.arange(1, 10, .1)
X, Y = np.meshgrid(x, y)
Z = f(X,Y)


f, ax = plt.subplots(figsize=(4, 4))
plt.pcolormesh(X, Y, Z)
plt.show()

output

enter image description here

CodePudding user response:

Anwered here You can remove the margins at the edges of the plot.

plt.margins(x=0)
  • Related