PyQtGraph - Setting Vertical Range of Plot Window

PyQtGraph - Setting Vertical Range of Plot Window

In PyQtGraph, a fast data visualization library built on PyQt, setting the vertical range of a plot window is straightforward. You can use the setYRange method of the plot widget to specify the minimum and maximum values of the Y-axis. Here��s how you can do it:

Basic Setup

First, ensure you have PyQtGraph and PyQt installed. If not, you can install them using pip:

pip install pyqtgraph PyQt5 

Creating a Plot and Setting the Y-Axis Range

Here's a simple example to demonstrate creating a plot and setting its vertical range:

  1. Import necessary modules:

    import pyqtgraph as pg from PyQt5 import QtWidgets import sys 
  2. Create a PyQt application:

    app = QtWidgets.QApplication(sys.argv) 
  3. Create a plot window:

    plotWidget = pg.plot(title="Setting Vertical Range in PyQtGraph") 
  4. Set the vertical range:

    To set the vertical range, use the setYRange method. For example, to set the range from -10 to 10:

    plotWidget.setYRange(-10, 10) 
  5. Add some data to the plot (optional):

    import numpy as np x = np.arange(100) y = np.random.normal(size=100) plotWidget.plot(x, y) 
  6. Show the plot window:

    plotWidget.show() 
  7. Run the application:

    sys.exit(app.exec_()) 

Full Example

Putting it all together, the complete script would look like this:

import sys import numpy as np import pyqtgraph as pg from PyQt5 import QtWidgets # Create a PyQt application app = QtWidgets.QApplication(sys.argv) # Create a plot window plotWidget = pg.plot(title="Setting Vertical Range in PyQtGraph") # Set the vertical range of the plot plotWidget.setYRange(-10, 10) # Add some random data x = np.arange(100) y = np.random.normal(size=100) plotWidget.plot(x, y) # Show the plot window plotWidget.show() # Run the application sys.exit(app.exec_()) 

This script creates a PyQtGraph window with the Y-axis range set from -10 to 10 and plots some random data on it. You can customize the range, data, and other plot properties according to your needs.


More Tags

intentservice sniffing memory-address resthub uwp iso-8859-1 startup unique-constraint filebeat click-tracking

More Programming Guides

Other Guides

More Programming Examples