CHAPTER 12 - PYTHON PLOTTING



AD

12.1.0 A simple Python plot

We must import the matplotlib package using Pycharm package manager. The process is described in the section 12.2.0. Pyplot plots the graph, and numpy holds the variables.

# import matplotlib to plot graphs
import matplotlib.pyplot as plt
import numpy as np
days = [1,3,5,6,8,]
temp = [37,34,37,35,34]
plt.plot(days, temp)
plt.show()


The plot can be seen below. You can make the plot much fancier depending on your needs. There are many examples on the internet.

Sample Python Matplotlib graph.


12.2.0 Installing the Python matplotlib package

Press Ctrl → Alt → s in the PyCharm window. The Settings window will open. Under Project select Python Interpreter.

PyCharm Settings screen.


Click the plus sign (+) on the right. A new window will appear with the available packages. Type matplotlib next to the search icon. Be sure matplotlib is highlighted, and click Install Package.

PyCharm Available Packages screen.


You will see the following screen saying Package ‘matplotlib’ installed successfully. You can close the open windows, the package is installed.

PyCharm Package matplotlib installed successfully screen.

12.3.0 Problems installing matplotlib package

There are several reasons why a package doesn't install correctly. The simplest thing to try is uninstalling the matplotlib package, and reinstalling the package with a lower version number.

12.4.0 Problems running the plot program in PyCharm

Occasionally the program will hang for no reason. Use run->debug to view the plots.

12.5.0 The simple Python plot with axis information

The following example graph is the same as above, except we have added axis information.

import matplotlib.pyplot as plt
import numpy as np
days = [1,3,5,6,8,]
temp = [37,34,37,35,34]

fig, ax = plt.subplots()
ax.plot(days, temp)
ax.set(xlabel='DAYS (days)', ylabel='TEMP (temp)', title='DAYS vs. TEMP')
ax.grid()

plt.show()

Pycharm Package matplotlib plot with axis information.





Engineering-Python

SALARSEN.COM
Table of Contents
Ch1-Install Python
Ch2-Install PyCharm
Ch3-Save Work
Ch4-Add Project
Ch5-Variables
Ch6-Print&Input
Ch7-Lists
Ch8-Loops
Ch9-If&Logical
Ch10-Functions
Ch11-Bubble Sort
Ch12-Plotting
Ch13-Files
Ch14-Print Format
Ch15-Dict&Comp&Zip
Ch16-Arrays
Ch17-Electrical
Ch18-Regression
Ch19-Differential
Ch20-Secant