[Introduction to Python] Basic usage of the library matplotlib

[Introduction to Python] Basic usage of the library matplotlib

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.

Install matplotlib

Install numpy / scipy

matplotlib is often combined with numpy and scipy. It's not absolute, but if you need it, install numpy and scipy first.

Install matplotlib

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.

use matplotlib

Draw a graph

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 figure_1.png

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.

Graph settings

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 figure_2.png

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.

Display multiple graphs

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 figure_3.png

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 PythonReputation of Python books and reference books

Recommended Posts

[Introduction to Python] Basic usage of the library matplotlib
[Introduction to Python] Basic usage of lambda expressions
[Introduction to Python] Basic usage of the library scipy that you absolutely must know
Basic usage of Python f-string
Introduction to Python Numerical Library NumPy
[python] How to use the library Matplotlib for drawing graphs
Introduction of Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Introduction to Python] I compared the naming conventions of C # and Python.
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Python Basic Course (at the end of 15)
[Introduction to Data Scientists] Basics of Python ♬
Introduction of Python
[Python] How to specify the window display position and size of matplotlib
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Introduction to Python] Thorough explanation of the character string type used in Python!
[Introduction to Python] What is the method of repeating with the continue statement?
How to use the C library in Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
Introduction to Python Let's prepare the development environment
Python & Machine Learning Study Memo ②: Introduction of Library
Introduction of Python Imaging Library (PIL) using HomeBrew
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Introduction to Python with Atom (on the way)
This is the only basic review of Python ~ 3 ~
[Introduction to Algorithm] Find the shortest path [Python3]
From the introduction of pyethapp to the execution of contract
Basic usage of flask-classy
Basic usage of Jinja2
Usage of Python locals ()
Introduction to Python language
Basic usage of SQLAlchemy
Introduction to OpenCV (python)-(2)
Python Basic Course (Introduction)
Basic knowledge of Python
Installation of matplotlib (Python 3.3.2)
I wanted to visualize 3D particle simulation with the Python visualization library Matplotlib.
What you want to memorize with the basic "string manipulation" grammar of python
[Introduction to Python] How to get the index of data with a for statement
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
The wall of changing the Django service from Python 2.7 to Python 3
Template of python script to read the contents of the file
I tried to summarize how to use matplotlib of python
I tried to summarize the basic form of GPLVM
Add information to the bottom of the figure with Matplotlib
[Introduction to Python] How to iterate with the range function?
Python --Explanation and usage summary of the top 24 packages
[python] option to turn off the output of click.progressbar
[Chapter 5] Introduction to Python with 100 knocks of language processing
Make a note of the list of basic Pandas usage
I wanted to use the Python library from MATLAB
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Python] How to use the graph creation library Altair
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Python] Get the list of ExifTags names of Pillow library
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set