Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a vertical line using matplotlib

Tags:

matplotlib

I would like to draw a vertical line with Matpotlib and I'm using axvline, but it doesn't work.

import sys
import matplotlib
matplotlib.use('Qt4Agg')

from ui_courbe import *
from PyQt4 import  QtGui
from matplotlib import pyplot as plt


class Window(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setupUi(self) 
        self.boutonDessiner.clicked.connect(self.generatePlot)

    def generatePlot(self):
        # generate the plot 
        ax = self.graphicsView.canvas.fig.add_subplot(111)
        ax.plot([1,3,5,7],[2,5,1,-2])
        plt.axvline(x=4)
        self.graphicsView.canvas.draw()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

I can see my plot, but no vertical line. Why?

like image 349
Philippe Juhel Avatar asked Mar 17 '15 10:03

Philippe Juhel


People also ask

How do you draw a vertical line in Python?

By using axvline() In matplotlib, the axvline() method is used to add vertical lines to the plot. The above-used parameters are described as below: x: specify position on the x-axis to plot the line. ymin and ymax: specify the starting and ending range of the line.

How do I create a vertical and horizontal line in matplotlib?

The method axhline and axvline are used to draw lines at the axes coordinate. In this coordinate system, coordinate for the bottom left point is (0,0), while the coordinate for the top right point is (1,1), regardless of the data range of your plot. Both the parameter xmin and xmax are in the range [0,1].

How do you plot a vertical line in Matlab?

xline( x ) creates a vertical line at one or more x-coordinates in the current axes. For example, xline(2) creates a line at x=2 . xline( x , LineSpec ) specifies the line style, the line color, or both.


2 Answers

Your example is not self contained, but I think you need to replace:

plt.axvline(x=4)

with:

ax.axvline(x=4)

You are adding the line to an axis that you are not displaying. Using plt. is the pyplot interface which you probably want to avoid for a GUI. So all your plotting has to go on an axis like ax.

like image 63
tobias47n9e Avatar answered Oct 17 '22 00:10

tobias47n9e


matplotlib.pyplot.vlines

  • The difference is that you can pass multiple locations for x as a list, while matplotlib.pyplot.axvline only permits one location.
    • Single location: x=37
    • Multiple locations: x=[37, 38, 39]
  • If you're plotting a figure with something like fig, ax = plt.subplots(), then replace plt.vlines or plt.axvline with ax.vlines or ax.axvline, respectively.
import numpy as np
import matplotlib.pyplot as plt

xs = np.linspace(1, 21, 200)
plt.vlines(x=[37, 38, 39], ymin=0, ymax=len(xs), colors='purple', ls='--', lw=2, label='vline_multiple')
plt.vlines(x=40, ymin=0, ymax=len(xs), colors='green', ls=':', lw=2, label='vline_single')
plt.axvline(x=36, color='b', label='avline')
plt.legend()
plt.show()

enter image description here

like image 20
Trenton McKinney Avatar answered Oct 17 '22 02:10

Trenton McKinney