# Matplotlib set axis range

In this Python Matplotlib tutorial, we’ll discuss the Matplotlib set axis range. Here we’ll cover different examples related to the set axis range using matplotlib. And we’ll also cover the following topics:

• Matplotlib set axis range
• Matplotlib set axis range 3d
• Matplotlib set axis range subplot
• Matplotlib set axis limits for all subplots
• Matplotlib set axis range scatter
• Matplotlib set axis range datetime
• Matplotlib imshow set axis range
• Matplotlib set axis same scale
• Matplotlib set secondary axis range
• Matplotlib 3d scatter set axis range
• Matplotlib set axis scale log
• Matplotlib set axis lower limit

## Matplotlib set axis range

Matplotlib is one of Python’s most popular data visualization libraries. In this section, we’ll look at how to use Matplotlib’s axis range to truncate or expand certain boundaries of the plot.

Matplotlib generates the minimum and maximum values of variables to be presented along the x, y (and z-axis in the case of a 3D plot) axes of a plot automatically.

It is possible, however, to define explicit limits by using the following methods:

• By using xlim() and ylim()
• By using set_xlim() and set_ylim()
• By using axis()

### By using xlim() and ylim() methods

In matplotlib, to set or get X-axis and Y-axis limits, use xlim() and ylim() methods, accordingly. These methods define the limits for respective axes if arguments are passed, and if no arguments are passed, we obtain a range of the respective axes.

The following is the syntax:

``````# Set x-axis range

matplotlib.pyplot.xlim()

# Set y-axis range

matplotlib.pyplot.ylim()``````

Let’s see examples:

Example #1

In this example, we didn’t use xlim() and ylim() functions, we would get a plot with the full range.

``````# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(0, 10, 0.2)
y = np.sin(x)

# Plot

plt.plot(x, y)

plt.title("Plot without limiting axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````

As seen in the output, we would get a plot with the complete range of axes, with the X-axis ranging from -1 to 1 and the Y-axis ranging from 0 to 10.

Example #2

In this example, we use xlim() and ylim() functions, to get a plot with manually selected limits.

``````# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(0, 10, 0.2)
y = np.sin(x)

# Plot

plt.plot(x, y)

# Set axes limit

plt.xlim(2, 8)
plt.ylim(-0.50,1.5)

plt.title("Setting range of Axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````
• Firstly, import necessary libraries such as numpy and matplotlib.pyplot.
• To define x-axis and y-axis data coordinates, use arange() and sin() functions.
• To plot a line graph, use the plot() function.
• To set range of x-axis and y-axis, use xlim() and ylim() function respectively.
• To add a title to the plot, use the title() function.
• To add label at axes, use xlabel() and ylabel() functions.
• To visualize the plot, use the show() function.

This sets the range of the X-axis from 2 to 8 while that of the Y-axis is from -0.50 to 1.5.

### By using set_xlim() and set_ylim() methods

The set_xlim() and set_ylim() functions are also used to limit the range of numbers on the plot.

The following is the syntax:

``````# Set x-axis range

matplotlib.axes.Axes.set_xlim()

# Set y-axis range

matplotlib.axes.Axes.set_ylim()``````

Let’s see examples:

Example #1

In this example, we didn’t use set_xlim() and set_ylim() functions, to get a plot with the full range.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.random.randint(low = -15, high = 80, size = 50)

# Plot

plt.plot(x, color='#EE1289')

plt.title("Plot without limiting axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````

As seen in the output, we would get a plot with the complete range of axes, with the X-axis ranging from 0 to 80 and the Y-axis ranging from 0 to 50.

Example #2

In this example, we use set_xlim() and set_ylim() functions, to get a plot with manually selected limits.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.random.randint(low = -15, high = 80, size = 50)

# Plot

plt.plot(x, color='#EE1289')

# Set axes limit

plt.xlim(0 , 43)
plt.ylim(0 , 55)

plt.title("Setting range of Axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````
• Import matplotlib.pyplot library.
• After this, import the numpy package.
• Then, define the data coordinate using rndom.randint() method of numpy.
• To add axes to the current figure, use the axes() method.
• To set axes limits use set_xlim() and set_ylim() for x-axis and y-axis respectively.
• To plot a line chart, use the plot() function of pyplot module.
• To add a title to the plot, use the title() function.
• To add a label at the x-axis, use the xlabel() function.
• To add a label at the y-axis, use the ylabel() function.
• To display the graph on the user’s screen, use the show() function.

This sets the range of the X-axis from 0 to 43 while that of the Y-axis is from 0 to 55.

### By using axis() method

To set limits of axes, we could also use the axis() function.

The following is the syntax:

``matplotlib.pyplot.axis([xmin, xmax, ymin, ymax])``

Let’s see examples:

Example #1

In this example, we didn’t use the axis() function, to get a plot with the full range.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.linspace(0, 8, 100)
y = np.exp(x/2)

# Plot

plt.plot(x, y, color='green')

plt.title("Plot without limiting axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````

As seen in the output, we would get a plot with the complete range of axes, with the X-axis ranging from 0 to 50 and the Y-axis ranging from 0 to 8.

Example #2

In this example, we use the axis() function, to get a plot with manually selected limits.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.linspace(0, 8, 100)
y = np.exp(x/2)

# Set axes

plt.axis([2, 6.5, 2, 45])

# Plot

plt.plot(x, y, color='green')

plt.title("Setting range of Axes")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````
• Import the matplotlib.pyplot library into your project.
• Import the numpy package after that.
• Then, using numpy’s linspace() and exp() methods, define the data coordinates.
• Use the axis() function to set axes limits for the x- and y-axes.
• Use the pyplot module’s plot() function to create a line chart.
• Use the title() function to add a title to the plot.
• Use the xlabel() function to add a label on the x-axis.
• Use the ylabel() function to add a label to the y-axis.
• Use the show() function to display the graph on the user’s screen.

This sets the range of the X-axis from 2 to 6.5 while that of the Y-axis is from 2 to 45.

## Matplotlib set axis range 3d

We’ll learn how to adjust the axis limit of a 3D plot in this tutorial. The range of values on the axes is automatically defined by the input values.

The set_xlim(), set_ylim(), and set_zlim() functions are used to change the minimum and maximum limits on each axis.

The following is the syntax for changing axis limits:

``````# For x-axis limit
matplotlib.axis.Axis.set_xlim(min, max)

# For y-axis limit
matplotlib.axis.Axis.set_ylim(min, max)

# For z-axis limit
matplotlib.axis.Axis.set_zlim(min, max)``````

Let’s see examples related to this:

Example #1

In this example, we’ll plot a 3D line chart and modify its axes by using above defined functions.

``````# Import libraries

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

# 3D projection

ax = plt.axes(projection ="3d")

# Define Data

x = np.arange(20)
y = x+8
z = 2*x+8

# Modify axis

ax.set_xlim(0,20)
ax.set_ylim(5,32)
ax.set_zlim(10, 55)

# Plot

ax.plot3D(x, y, z)

# Show

plt.show()``````
• Import mplot3d library for 3d projection.
• Next, import the numpy library for data creation.
• After this, import matplotlib.pyplot for visualization of graphs.
• To set 3d projection, add axes to the new figure by using axes() method and pass projection parameter and set to 3d.
• Next, define data coordinates.
• Then, use set_xlim()set_ylim()set_zlim() methods to modify the limits for three axes based on min and max values passed.
• To polt the 3d line chart, use plot3d() function.

Example #2

In this example, we’ll plot a 3D bar chart and modify its axes by using above defined functions.

``````# Import libraries

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

# 3D projection

ax = plt.axes(projection ="3d")

# Define Data

x3 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
y3 = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
z3 = np.zeros(10)

dx = np.ones(10)
dy = np.ones(10)
dz = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]

# Modify axis

ax.set_xlim(0,30)
ax.set_ylim(5, 70)
ax.set_zlim(-2, 55)

# Plot

ax.bar3d(x3, y3, z3, dx, dy, dz)

# Show

plt.show()``````
• Import necessary libraries such as mplot3d, numpy, and matplotlib.pyplot.
• To define 3D projection add axes to the new figure by using the axes() method and pass projection parameter and set to 3d.
• To define the width, depth, and height use the zero() and ones() method of numpy.
• Then, use set_xlim()set_ylim()set_zlim() methods to modify the limits for three axes based on min and max values passed.
• To polt the 3d bar chart, use bar3d() function.

Also, check: What is add_axes matplotlib

## Matplotlib set axis range subplot

Here we’ll learn to set the axis range of the specific subplot using matplotlib.

Let’s see examples related to this:

Example #1

Here we use xlim() and ylim() methods to modify axes limits of specific subplots.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# plot 1:

plt.subplot(1, 2, 1)

# Data

x = np.arange(0,10,0.2)
y = np.sin(x)

# Plotting

plt.plot(x, y)

# plot 2:

plt.subplot(1, 2, 2 )

# Data

x = np.arange(0,10,0.2)
y = np.cos(x)

# Set axes for specific subplot

plt.xlim(2, 8)
plt.ylim(-0.50,1.5)

# Plotting

plt.plot(x,y)

plt.tight_layout()

# Display

plt.show()``````
• Import matplotlib.pyplot and numpy libraries.
• Next, to create a subplot use subplot() function with defined rows and columns.
• The arange(), sin(), and cos() methods are used to define data coordinates.
• Here we modify axes of subplot 2nd, so we use xlim()and ylim() functions.
• To plot a graph, use the plot() function in each subplot.

Example #2

Here we’ll create two subplots one with original limit values and another with truncated limits values.

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Set figure size

fig = plt.figure(figsize=(12, 6))

# Define Data

x = np.arange(0, 10, 0.1)
y = np.tan(x)

# Create subplot

# Set limits

ax2.set_xlim([25, 50])
ax2.set_ylim([-20, 60])

# Plot graphs

ax1.set_title('Orignal Limits')
ax1.plot(y, color='blue')

ax2.set_title('Truncated Limits')
ax2.plot(y, color='blue')

# Display

plt.show()``````
• Import necessary libraries such as matplotlib.pyplot and numpy.
• To set figure size, use the figure() method and pass figsize parameter to it.
• To define data coordinates, use arange() and tan() method.
• To create subplot, use add_subplot() method.
• To set limits, use set_xlim() and set_ylim() method.
• To add the title to the plot, use the set_title() method.
• To plot the graph, use the plot() method.

## Matplotlib set axis limits for all subplots

Here we’ll learn to set the same axis limits for all subplots using matplotlib with the help of examples.

Let’s see examples:

Example #1

Here, we’ll use set_xlim() and set_ylim() method to set the same limits for all subplots axes.

``````# Import Libraries

import matplotlib.pyplot as plt

# Set figure size

plt.figure(figsize=(8, 7))

# Subplot 1

ax1 = plt.subplot(2, 2, 1)

# Set axes

ax1.set_xlim(2, 15)
ax1.set_ylim(0, 25)

ax1.plot([1, 2, 3, 8, 16, 21], [2, 4, 1, 20, 13, 15])

# Subplot 2

ax2 = plt.subplot(2, 2, 2, sharey=ax1, sharex=ax1)
ax2.plot([4, 2, 6, 8, 3, 20, 13, 15])

# Subplot 3

ax3 = plt.subplot(2, 2, 4, sharey=ax1, sharex=ax1)
ax3.plot([1, 2, 3, 4, 5])

# Subplot 4

ax4 = plt.subplot(2, 2, 3, sharey=ax1, sharex=ax1)
ax4.plot([0, 0.6, 0.2, 15, 10, 8])

# auto layout

plt.tight_layout()

# Display

plt.show()``````
• Firstly, import matplotlib.pyplot, and numpy libraries.
• Next, set the size of the figure by using the figure() method and passing figsize parameter.
• To create a subplot in the figure at index 1, 2, 3, 4 respectively by using the subplot() method.
• To set the axes limits, use set_xlim() and set_ylim() methods.
• To share the same axes, use sharex and sharey parameters.
• To plot lines at different axes, use the plot() method.
• To show the figure, use the show() function.

Example #2

In this example, we use the setp() function to set the same axis limits to all subplots.

``````# Import Library

import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots(2, 2)

# Define Data

x1 = [1, 2, 3, 8, 16, 21]
y1 = [2, 4, 1, 20, 13, 15]

x2 = [4, 2, 6, 8, 3, 20, 13, 15]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

# Define axes

cust_xlim = (0, 30)
cust_ylim = (2, 30)

# Setting the same values for all axes

plt.setp(ax, xlim=cust_xlim, ylim=cust_ylim)

# Plot

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4, y4)

fig.tight_layout()

# Display

plt.show()``````
• Import matplotlib.pyplot library.
• To create subplots, use subplots() method and specify rows and columns as a parameter. Here we set it to 2 rows and 2 columns.
• Next, define data coordinates for plotting.
• Then, define the axes limits.
• To set the same axes limit value for all the subplots, use setp() method.
• To plot the graph, we use plot() function.
• To auto adjust the subplots, use tight_layout() function.
• To display the plots, use show() method.

## Matpolotlib set axis range scatter

Here, we’ll learn to set the axis range of scatter plot using matplotlib.

READ:  NumPy zeros in Python [6+ Examples]

Let’s see examples:

Example #1

In this example, we use the axis() method to set the axis range.

``````# Import Library

import matplotlib.pyplot as plt

# Define Data

x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]

y =[99, 86, 87, 88, 100, 86,
103, 87, 94, 78, 77, 85, 86]

# Set axes

plt.axis([0, 15, 50, 120])

# Plot

plt.scatter(x, y, color='green')

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````
• Firstly, import matplotlib.pyplot library for data visualization.
• Next, define the data coordinates.
• To set axes of the scatter plot, use axis() method and set xmin, xmax. ymin, and ymax values respectively.
• To plot the scatter graph, use scatter() function.
• To set label at the x-axis, use xlabel() function.
• To set label at y-axis, use ylabel() function.
• To view the graph, use show() method.

Example #2

In this example, we use xlim() and ylim() functions to set the range of axes.

``````# Import Library

import matplotlib.pyplot as plt

# Define Data

x = np.random.randint(low = -20, high = 80, size = 100)
y = np.random.randint(low = 0, high = 50, size = 100)

# Set axes

plt.xlim(0, 60)
plt.ylim(10, 40)

# Plot

plt.scatter(x, y, color='blue')

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()
``````
• Firstly, import matplotlib.pyplot library for data visualization.
• Next, define the data coordinates by using random.randint() function.
• To set axes of the scatter plot, use xlim() and ylim() functions.
• To plot the scatter graph, use scatter() function.
• To set label at the x-axis, use xlabel() function.
• To set label at y-axis, use ylabel() function.
• To view the graph, use show() method.

## Matplotlib set axis range datetime

Here, we’ll learn to set the axis range of the datetime plot using matplotlib.

Let’s see an example related to this:

``````# Import Libraries

import datetime
import matplotlib.pyplot as plt

# Create subplot

fig, ax = plt.subplots()

# Define Data

x = [datetime.date(2022, 1, 30)] * 5
y = [2, 4, 1, 6, 8]

# Set axes

ax.set_xlim([datetime.date(2022, 1, 1), datetime.date(2022, 2,
1)])
ax.set_ylim([0, 10])

# Plot date

ax.plot_date(x, y, markerfacecolor='m', markeredgecolor='k',
markersize= 15)

# Auto format

fig.autofmt_xdate()

# Display

plt.show()``````
• Import necessary libraries such as datetime, and matplotlib.pyplot.
• To create a subplot, use subplots() function.
• To define data coordinates, use datetime.date() function.
• To set axes, use set_xlim() and set_ylim() functions.
• To plot the date time graph, use plot_date() function.
• To set marker face color, edge color and size pass markerfacecolor, markeredgecolor and markersize as parameter.
• To auto formate the dates at x-axis, use autofmt_xdate() function.
• To view the plot, use show() function.

## Matplotlib imshow set axis range

Here we’ll learn to set the axis range of imshow using matplotlib. To change the axis of imshow, we have to pass an extent argument to imshow() function.

The following is the syntax:

``matplotlib.pyplot.imshow(extent = [x_min, x_max, y_min, y_max])``

Let’s see examples:

Here we set axes by using the extent parameter.

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(400).reshape((20,20))

# Imshow set axes

plt.imshow(x , extent=[-1,1,-1,1])

plt.title( "Imshow Plot" )

# Display

plt.show()``````
• Import numpy libaray as np.
• Import matplotlib.pyplot library as plt.
• To define the data coordinate, use arange() and reshape() method.
• To plot imshow plot, use imshow() plot.
• To set axes limits, pass extent argument to method with x_min, x_max, y_min, and y_max values respectively.
• To add a title, use title() function.

We can also set axes of imshow by using xlim(), ylim() methods or by using set_xlim() and set_ylim() methods or by using axis() method.

Let’s see one more example to set axes of imshow by using the above define method:

Here we use the axis() method to set axes of imshow.

Example #2

``````# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [[1, 2, 4, 5, 6, 7],
[21, 22, 24, 25, 26, 27],
[100, 13, 41, 51, 61, 71],
[111, 121, 141, 151, 161, 171]]

# Set axes

plt.axis([1, 4, 1, 3])

# Imshow Plot

plt.imshow(x)

plt.title( "Imshow Plot" )

# Display

plt.show()``````
• Import matplotlib.pyplot library.
• Next, define data coordinates.
• To set axes, use axis() method.
• To plot imshow graph, use imshow() function.
• To add title, use title() function.
• To visualize the plot on user’s screen, use show() function.

## Matplotlib set axis same scale

Here we’ll learn how we can set the same scale axis limits at both axes i.e. x-axis and y-axis using matplotlib.

Example:

``````# Import Library

import matplotlib.pyplot as plt

# Define Data

x = np.random.randint(low = -15, high = 80, size = 50)

# Plot

plt.plot(x)

# Set Axes

plt.xlim(0, 60)
plt.ylim(0, 60)

# Display

plt.show()``````
• Import matplotlib.pyplot library for data visualization.
• Next, define the data coordinate. Here we use random.randint() function to define data.
• To plot the graph, use the plot() function.
• To set the limit of the x-axis, use the xlim() function.
• To set the limit of the y-axis, use the ylim() function.
• plt.gca() function is used to get the current axes.
• Next, we use the set_aspect() function, to set the aspect of the axis scaling. Here we set aspect to equal which means the same scaling from data to plot units for x and y.

## Matplotlib set secondary axis range

Here we’ll learn to set secondary axis range using matplotlib.

Let’s see examples related to this:

Example #1

Here we are going to create two y-axes and set their axes range using set_xlim() and set_ylim() functions.

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Set figure size

fig, ax = plt.subplots(figsize = (8,5))

# Define Data Coordinates

x = np.arange(0, 50, 2)
y1 = np.sin(x*20)
y2 = x**2

# Title

plt.title('Set Secondary axis range')

# Create Secondary Axes

ax2 = ax.twinx()
ax.plot(x, y1, color = 'g')
ax2.plot(x, y2, color = 'b')

# Set axes range

ax.set_xlim(0,45)
ax.set_ylim(-1, 1)
ax2.set_ylim(50, 3000)

# Set Labels

ax.set_xlabel('x-axis', color = 'r')
ax.set_ylabel('Primary y-axis', color = 'g')
ax2.set_ylabel('Secondary y-axis', color = 'b')

# Show plot

plt.show()``````
• Import numpy library for data creation.
• Import matplotlib.pyplot library for data visualization.
• To plot a figure by creating axes objects, use the subplots() function.
• To set the size of a plot, use figsize parameter and set width and height.
• To define data coordinates, we use arange() and sin() function of numpy.
• To set the title of a plot, we use the title() function.
• To create a secondary y-axis, we use twinx() function.
• To plot data corresponding to axes, we use the plot() function.
• To differentiate between the plotted data, pass color argument and set different colors.
• To set axes range of primary as well as secondary axes, we use set_xlim() and set_ylim() function.
• To set label at the x-axis, we use set_xlabel() function.
• To set labels at the primary and secondary y-axis, we use the set_ylabel() function.
• We also set the color of labels at each ax, bypassing the color parameter to the label’s functions.
• To display the plot, we use the show() function.

Example #2

READ:  Python Tkinter Editor + Examples

Here we are going to create two x-axes and set their axes range using set_xlim() and set_ylim() functions.

``````# Import library

import matplotlib.pyplot as plt

# Create subplot

fig, ax1 = plt.subplots()

# Plot

ax1.plot([1, 2, 3, 4, 5], [2, 4, 12, 8, 10], color='red')

# Secondary x-axis

ax2 = ax1.twiny()
ax2.plot([5, 10, 15, 20, 25], [13, 51, 17, 11, 76], color='blue')

# Set axes limit

ax1.set_xlim(0, 4)
ax2.set_xlim(5, 20)
ax1.set_ylim(0, 60)

# Display

plt.show()``````
• Firstly, import matplotlib.pyplot library.
• To plot a figure by creating axes objects, use the subplots() function.
• To plot a graph, we use the plot() function.
• To create a secondary x-axis, we use twiny() function.
• To set the x-axis limits of both primary and secondary axes, we use the set_xlim() function.
• To set the y-axis limit, we use the set_ylim() function.
• To display the plot, we use the show() function.

## Matplotlib 3d scatter set axis range

We’ll learn how to adjust the axis limit of a 3D scatters plot in this tutorial. The range of values on the axes is automatically defined by the input values.

Let’s see examples related to this:

Example #1

In this example, we create 3d scatter plot and set their axes range using set_xlim3d(), set_ylim3d(), and set_zlim3d() functions.

``````# Import libraries

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

# 3D projection

ax = plt.axes(projection ="3d")

# Define Data

x = np.random.randint(low = -15, high = 80, size = 50)
y = np.random.randint(low = 0, high = 100, size = 50)

# Modify axis

ax.set_xlim3d(-2, 62)
ax.set_ylim3d(5, 70)
ax.set_zlim3d(-1, 1)

# Plot

ax.scatter3D(x,y, s=45, edgecolor='k', color='slategrey')

# Show

plt.show()``````
• Import mplot3d library of python for 3d projection.
• Next, import matplotlib.pyplot library for data visualization.
• After this, import numpy library of python for data creation.
• Add axes to the new figure, we use axes() method.
• To set 3d projection pass projection parameter to the method and set it to 3d.
• To define data coordinates, we use random.randint() function of numpy.
• To set the limit of x-axis, we use set_xlim3d() method.
• To set the limit of y-axis, we use set_ylim3d() method.
• To set the limit of z-axis, we use set_zlim3d() method.
• To plot a 3d scatter graph, we use scatter3D() function.
• The size, edgecolor and color parameter are used to beautify the plot.
• To display the plot, use show() method.

Example #2

In this example, we use the xlim() and ylim() function of the pyplot module to set the x-axis and y-axis limits. And to set the limit of the z-axis, we use the set_zlim() function.

``````# Import libraries

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

# Set figure size

plt.figure(figsize=(8,8))

# 3D projection

ax = plt.axes(projection ="3d")

# Define Data

z = np.random.randint(100, size =(200))
y = np.sin(z) + np.random.randint(100, size =(200))
x = np.cos(z) + np.random.randint(100, size =(200))

# Plot

ax.scatter3D(x, y, z, s=50, edgecolor='k', color='lightgreen', marker='>')

# Modify axis

plt.xlim(-1, 150)
plt.ylim(-1, 150)
ax.set_zlim(-1, 100)

# Show

plt.show()
``````
• First, import necessary libraries such as mplot3d, numpy and matplotlib.pyplot.
• Next, set the size of the figure, by using figure() method and passing figsize parameter.
• To set 3d projection, use the axes() method to add axes to a new figure and then set the projection parameter to 3d.
• By using random.randint(), sin(), and cos() methods define the data coordinates.
• To create a 3d scatter plot, use scatter3D() method.
• To set the limits of x-axis and y-axis, we use xlim() and ylim() function.
• To set the limit of z-axis, we use set_zlim() method.
• To display the plot, use show() function.

## Matplotlib set axis scale log

Here we’ll learn to set the axis limit of log scale using matplotlib.

Let’s see examples related to this:

Example #1

In this example, we create a plot with a y-axis log scale and set their limits.

``````# Import Library

import matplotlib.pyplot as plt

# Define data

data = [10**i for i in range(50)]

# Set y-axis log scale

plt.yscale("log")

# Plot

plt.plot(data)

# Set axes limits

plt.axis([5, 45, 1E10, 1E50])

# Display

plt.show()``````
• Import matplotlib.pyplot library for data visualization.
• Then define exponent data coordinates.
• To set log scale at y-axis, use yscale() function snd set it to log.
• To plot the graph, use plot() function.
• To set the axes limit, use axis() function.
• To display the plot, use show() function.

Example #2

Here we use xlim() and ylim() methods to set axes limits of log scale plot.

``````# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [ 10**i for i in range(50)]
y = [ i for i in range(50)]

# Log scale

plt.xscale("log")

# Plot

plt.plot(x,y)

# Set limits

plt.xlim(1E10, 1E40)
plt.ylim(10, 45)

# Display

plt.show()``````
• Import matplotlib.pyplot library for data visualization.
• Next, define data coordinates.
• To set x-axis scale to log, use xscale() function and pass log to it.
• To plot the graph, use plot() function.
• To set the limits of the x-axis, use xlim() function and pass max and min value to it.
• To set the limits of the y-axis, use ylim() function and pass top and bottom value to it.
• To display the graph, use show() function.

## Matplotlib set axis lower limit

Here we learn to set axis lower limit using matplotlib. The lower limit of the y-axis is the bottom and the lower limit of the x-axis is left.

Let’s see an example:

``````# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 50, 2)
y = np.sin(x)

# Set axes

plt.xlim(left=5)
plt.ylim(bottom=10)

# Plot

plt.plot(x, color='blue')

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

# Display

plt.show()``````
• Import matplotlib.pyplot and numpy library.
• Next, define the data coordinates, using arange() and sin() method of numpy.
• To set the lower axis limit, we use xlim() and ylim() function and pass the left and bottom parameter respectively.
• To plot the graph, use plot() function.
• To set the labels at the axes, use xlabel() and ylabel() function.
• To display the graph, use show() function.

You may also like to read the following Matplotlib tutorials.

So, in this Python tutorial, we have discussed the “Matplotlib set axis range” and we have also covered some examples related to using set axis range matplotlib. These are the following topics that we have discussed in this tutorial.

• Matplotlib set axis range
• Matplotlib set axis range 3d
• Matplotlib set axis range subplot
• Matplotlib set axis limits for all subplots
• Matplotlib set axis range scatter
• Matplotlib set axis range datetime
• Matplotlib imshow set axis range
• Matplotlib set axis same scale
• Matplotlib set secondary axis range
• Matplotlib 3d scatter set axis range
• Matplotlib set axis scale log
• Matplotlib set axis lower limit