[Python] How to draw a line graph with Matplotlib

Use plot to draw a line graph. Some examples are shown below.

Drawing a simple line graph

Pass the x, y values separately, like plot (x, y). The line style can be specified with the parameter linestyle.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

x = np.linspace(-6,6,1000)

ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black',  linestyle='solid')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='black',  linestyle='dashed')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='black', linestyle='dashdot')

ax.set_title('First line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()

plot_01.png

Change line thickness

You can change the line thickness with linewidth.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

x = np.linspace(-6,6,1000)

ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black',  linestyle='solid', linewidth = 3.0, label='line1')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='black',  linestyle='dashed',linewidth = 1.0, label='line2')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='black', linestyle='dashdot', linewidth = 0.5,label='line3')

ax.set_title('Second line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
ax.grid(True)
fig.show()

plot_02.png

Change line color

Change the line color with color.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

x = np.linspace(-6,6,1000)

ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='red',  linestyle='solid', linewidth = 1.0)
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='green',linestyle='solid',linewidth = 1.0)
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='blue', linestyle='solid', linewidth = 1.0)

ax.set_title('Third line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()

plot_03.png

Draw marker

When marker is specified, marker is drawn at the data location of the line graph. When there is a lot of data, the line disappears with the marker like the line below. By specifying markevery, you can specify how many markers are drawn at intervals.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

x = np.linspace(-6,6,1000)

ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black',  linestyle='solid', linewidth = 1.0, marker='o')
ax.plot(x,0.5 + norm.pdf(x, loc=0.0, scale=1.0), color='black',  linestyle='solid', linewidth = 1.0, marker='o', markevery = 50)

ax.set_title('4th line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()

plot_04.png

Typical of marker

marker description
. point
o circle
v Lower triangle
^ Upper triangle
s square
+ plus
x cross
* star

http://matplotlib.org/api/markers_api.html

Recommended Posts

[Python] How to draw a line graph with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
How to draw a graph using Matplotlib
How to draw a 2-axis graph with pyplot
[Python] How to draw multiple graphs with Matplotlib
[Python] How to draw a histogram in Matplotlib
Draw a loose graph with matplotlib
How to draw a bar graph that summarizes multiple series with matplotlib
How to draw a vertical line on a heatmap drawn with Python seaborn
[Python] How to create a 2D histogram with Matplotlib
Study math with Python: Draw a sympy (scipy) graph with matplotlib
How to install NPI + send a message to line with python
Draw a flat surface with a matplotlib 3d graph
How to send a message to LINE with curl
How to draw a 3D graph before optimization
[Python] Draw a directed graph with Dash Cytoscape
Try to draw a life curve with python
Draw a graph with matplotlib from a csv file
Draw a graph with NetworkX
(Matplotlib) I want to draw a graph with a size specified in pixels
Draw a graph with networkx
How to convert / restore a string with [] in python
I tried to draw a route map with Python
Forcibly draw something like a flowchart with Python, matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
How to write a Python class
A python graphing manual with Matplotlib.
Draw a graph with Julia + PyQtGraph (2)
Draw a graph with Julia + PyQtGraph (1)
How to get started with Python
Draw a graph with Julia + PyQtGraph (3)
How to use FTP with Python
Draw a graph with pandas + XlsxWriter
How to calculate date with python
Let's make a graph with python! !!
Easy to draw graphs with matplotlib
Draw Lyapunov Fractal with Python, matplotlib
Draw a graph with PySimple GUI
How to convert an array to a dictionary with Python [Application]
How to build a python2.7 series development environment with Vagrant
[Visualization] I want to draw a beautiful graph with Plotly
How to batch start a python program created with Jupyter notebook
How to title multiple figures with matplotlib
How to add a package with PyCharm
[Python] How to make a class iterable
[Introduction to Python] How to split a character string with the split function
[Python] How to convert a 2D list to a 1D list
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to make a surveillance camera (Security Camera) with Opencv and Python
How to get a string from a command line argument in python
How to work with BigQuery in Python
[Python] How to invert a character string
How to create a heatmap with an arbitrary domain in Python
How to get a stacktrace in python
How to do portmanteau test with python
How to display python Japanese with lolipop
[Python] How to get a value with a key other than value with Enum
How to enter Japanese with Python curses
[Python] How to deal with module errors
[ROS2] How to play a bag file with python format launch