Main Menu

search

You are here

Python: Matplotlib

last updated: 2021-07-06]
go to: Python Home page

(link to:) matplotlib.org - troubleshooting
-----

  • Install it with:
    $ pip install matplotlib
      (See here to verify that pip is installed)

  • Verify matplotlib is installed:
    Run this python program:
      #!/usr/bin/python
      import matplotlib
      print ('matplotlib version: ' + matplotlib.__version__)
      print ('prog end')
      # end program

  • If generating or analyzing your data requires calculations,
    it's recommended to use numpy,
    which is a library with support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
    Wikipedia

    pip install numpy
    ------------------------------------------

  • Demo program:
    (edited from: https://www.geeksforgeeks.org/graph-plotting-in-python-set-1/)
    code: demoPlot-03.py.txt
      Loaded onto Dell-4600
      Executed from terminal with:
      $ sudo python3 ~/Documents/myFiles/workingFiles-4600/Tech-some/Linux/python/testProgs/demoPlot-03.py

    ---------------------------------------------

  • Plot options:
    • The line in your python program that specifies and draws the plot is:
        plt.plot (x, y)

      This will create the plot using default parameter values.

    • Conversely, if you want to change things, you can specify values for plot parameters with:
        plt.plot (x, y, color='green', linestyle='solid', linewidth = 3,
        marker='o', markerfacecolor='blue', markersize=10)

    • Plot two lines:
      • Define two data sets:
        x1 = [1,2,3]
        y1 = [2,3,4]
        x2 = [1,2,3]
        y2 = [4,1,3]
      • Plot first line:
        plt.plot(x1, y1, label = "line 1")
      • Plot second line:
        plt.plot(x2, y2, label = "line 2")
      • Show a legend on the graph:
        plt.legend()

    • Lots of other options:
      • bar charts, pie charts, plus others
      • multiple plots on same figure
      • 3d plots
      • plus more...

    ---------------------------------------------

  • Animation:

.

.

.

eof