In Python, scientific calculations can be easily performed by using libraries such as numpy and scipy, but it is easy to understand if such calculation results can be charted in graphs. A library called matplotlib is useful in such cases. matplotlib allows you to plot data on a graph in Python. By combining with numpy and scipy, you can read, process, calculate, and plot data only with Python.
This time, I will explain the basic usage of matplotlib.
matplotlib is often combined with numpy and scipy. It's not absolute, but if you need it, install numpy and scipy first.
Install matplotlib. matplotlib can be installed with the pip command.
pip install matplotlib
Alternatively, download the file from the matplotlib page and install the downloaded file with the pip command.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
pip command
pip install the path to the file you just downloaded
Like numpy and scipy, matplotlib has several versions, so download the version that suits your python or OS. For example, the file "matplotlib-2.0.0-cp36-cp36m-win_amd64.whl" is for python3.6, 64-bit Windows.
After installing matplotlib, let's display the graph immediately. Use the matplotlib.pyplot module plot () and show () to display the graph.
from matplotlib import pyplot
pyplot.plot(x axis,y axis)
pyplot.show()
Pass a sequence (numpy array or list) as an argument to plot (). The first argument represents the x-axis and the second argument represents the y-axis. Plot the data with plot () and actually display it on the screen with show ().
import math
import numpy as np
from matplotlib import pyplot
pi = math.pi #Use π of math module
x = np.linspace(0, 2*pi, 100) #A numpy array with 100 divisions from 0 to 2π
y = np.sin(x)
pyplot.plot(x, y)
pyplot.show()
Execution result
In this example, the trigonometric function sin is calculated by numpy and the result is displayed by matplotlib. It's more visual and easier to understand than looking at numbers.
I was able to display the graph with plot () and show (), but there is little information in this graph. This example is a famous graph of sin, so it's easy for anyone to understand, but usually you need to set the title, axis name, legend, etc. to show what the graph represents.
With matplotlib, you can easily set the graph.
-Since it is the same as the above example, it is omitted.
#Give the legend a legend with the label keyword for the legend
pyplot.plot(x, y, label='sin')
#Graph title
pyplot.title('Sin Graph')
#Graph axis
pyplot.xlabel('X-Axis')
pyplot.ylabel('Y-Axis')
#Graph legend
pyplot.legend()
pyplot.show()
Execution result
In this example, the title, axis name, and legend are added to the graph of sin. When you give a legend, don't forget to give the legend name with the label keyword of plot (). There are many other settings for the graph, such as changing the line color and line type.
In the previous example, only the graph of sin was displayed, but you can display multiple data by adding data with plot ().
pi = math.pi
x = np.linspace(0, 2*pi, 100)
sin_y = np.sin(x)
cos_y = np.cos(x) #Newly calculate cos
pyplot.plot(x, sin_y, label='sin')
pyplot.plot(x, cos_y, label='cos') #Plot the value of cos
#Graph title
pyplot.title('Sin And Cos Graph')
#Graph axis
pyplot.xlabel('X-Axis')
pyplot.ylabel('Y-Axis')
#Graph legend
pyplot.legend()
pyplot.show()
Execution result
In this example, in addition to the previous sin, the graph of cos is also displayed. I was able to display two graphs at the same time. When displaying multiple graphs, don't forget to use the label keyword and legend to display the legend.
Recommended
➡ [Free to use] 7 learning sites where you can study Python ➡ Reputation of Python books and reference books
Recommended Posts