Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A partial axis can be created in matplotlib using the plt.axes() function. This function allows you to set the dimensions and position of the new axis relative to the original one. Here's an example:

import matplotlib.pyplot as plt

# create a figure and original axis
fig, ax = plt.subplots()

# set the dimensions and position of the new axis
partial_ax = plt.axes([0.25, 0.25, 0.5, 0.5])

# plot on the original axis
ax.plot([1, 2, 3], [1, 2, 3])

# plot on the partial axis
partial_ax.plot([1, 2], [0, 1])

# set labels for both axes
ax.set_xlabel('X')
ax.set_ylabel('Y')
partial_ax.set_xlabel('Partial X')
partial_ax.set_ylabel('Partial Y')

plt.show()

In this example, we create a figure and an original axis using the plt.subplots() function. Then we use the plt.axes() function to create a new axis that is a partial rectangle of the original one. We set the dimensions and position of this new axis relative to the original one by passing a list of values to the plt.axes() function. The list contains four values: the x-coordinate of the lower-left corner of the axis, the y-coordinate of the lower-left corner, the width of the axis, and the height of the axis, all relative to the original axis.

Finally, we plot on both axes and set labels for both of them using the set_xlabel() and set_ylabel() functions. When we call plt.show(), we see the original axis with a line plot and the partial axis with a shorter line plot, both with their respective labels.