Home > Backend Development > Python Tutorial > How to Customize Y-Axis Limits in Matplotlib Plots?

How to Customize Y-Axis Limits in Matplotlib Plots?

DDD
Release: 2024-12-16 01:40:14
Original
372 people have browsed it

How to Customize Y-Axis Limits in Matplotlib Plots?

Customizing Y-Axis Limits in Matplotlib

In matplotlib, customizing the axis limits is crucial for presenting data effectively. This question addresses the need to set the y-axis limits for a specific plot, where the provided code fails to achieve the desired limits.

To address this issue, we can use the following approach:

  1. Access the current axis using plt.gca():
ax = plt.gca()
Copy after login
  1. Set the y-axis limits using set_ylim():
ax.set_ylim([ymin, ymax])
Copy after login

In the example provided in the question, the code can be modified as follows:

import matplotlib.pyplot as plt

plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C',
     marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)

# Set y-axis limits
ax = plt.gca()
ax.set_ylim([20, 250])
Copy after login

With this adjustment, the y-axis limits will be set to [20, 250], as desired.

The above is the detailed content of How to Customize Y-Axis Limits in Matplotlib Plots?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template