Home > OS >  How to resize the plots to fit values in matplotlib
How to resize the plots to fit values in matplotlib

Time:06-03

I am trying to plot a series of small values through scatter plot on Raspberry Pi 3B. But when ever I try to plot those values they appear very small. I have tried out tight_layout() and axis('scaled') on both figure level and plot level, but nothing seems to work.

Code

u_lat, u_lon, u_list = [], [], [i for i in range(200)]
        for i in range(200):
            u_lat.append(random.uniform(17.160541, 17.161970))
            u_lon.append(random.uniform(78.658089, 78.660843))

        ##############################################################
        # Scatter plot of data
        userDataPlotFig, ax = plt.subplots()

        ax.scatter(u_lat, u_lon)

Plot Output enter image description here

I want it to be scaled properly to fit all the values on the screen.

CodePudding user response:

You need to set x, y axis limits. I give an example in which the necessary limits of the axes are commented out. If you replace the current ones with the one in the comments you will see the difference.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(70)
y = np.random.rand(70)

fig, ax = plt.subplots()

ax.scatter(x, y)
plt.xlim([0, 2])#plt.xlim([0, 1])
plt.ylim([0, 3])#plt.ylim([0, 1])
plt.show()

enter image description here

plt.xlim([0, 2])
plt.ylim([0, 3])

enter image description here

plt.xlim([0, 1])
plt.ylim([0, 1])

In your example, I suggest doing the following:

import numpy as np
import matplotlib.pyplot as plt

u_lat, u_lon, u_list = [], [], [i for i in range(200)]
for i in range(200):
    u_lat.append(np.random.uniform(17.160541, 17.161970))
    u_lon.append(np.random.uniform(78.658089, 78.660843))


lat = np.array(u_lat)
lon = np.array(u_lon)
min_x = np.min(lat)
max_x = np.max(lat)
min_y = np.min(lon)
max_y = np.max(lon)

userDataPlotFig, ax = plt.subplots()
ax.scatter(u_lat, u_lon)
plt.xlim([min_x, max_x])
plt.ylim([min_y, max_y])
plt.show()
  • Related