Home > Backend Development > Python Tutorial > python tutorial on using matplotlib to draw a line chart

python tutorial on using matplotlib to draw a line chart

高洛峰
Release: 2017-02-10 09:40:44
Original
3294 people have browsed it

Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can draw a variety of data graphics such as Matlab and Octave. The following article mainly introduces the tutorial on how to draw a line chart using matplotlib in python. Friends in need can refer to it.

Introduction to matplotlib

matplotlib is the most famous drawing library in Python. It provides a set of command APIs similar to matlab, which is very suitable for interaction. Cartographically. And it can also be easily used as a drawing control and embedded in GUI applications.

Its documentation is quite complete, and there are hundreds of thumbnails in the Gallery page, and there are source programs after opening them. So if you need to draw a certain type of diagram, just browse/copy/paste on this page and you'll basically be done.

The more famous data graph tool under Linux is gnuplot. This is free. Python has a package that can call gnuplot, but the syntax is not familiar and the drawing quality is not high.

Matplotlib is relatively strong: Matlab’s syntax, python language, and latex’s drawing quality (you can also use mathematical formulas drawn by the built-in latex engine).

How to install the drawing library Matplotlib: Click here

matplotlib draws a line chart

1. line chart

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1)
plt.plot(x, y2)

plt.title('line chart')
plt.xlabel('x')
plt.ylabel('y')

plt.show()
Copy after login

python tutorial on using matplotlib to draw a line chart

##2. Legend

Specify label when plotting, and then call the legend method to draw the legend. For example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, label='y = sin(x)')
plt.plot(x, y2, label='y = cos(x)')
plt.legend()
plt.show()
Copy after login

python tutorial on using matplotlib to draw a line chart

legend method accepts a loc keyword parameter to set the position of the legend. The value is a number or a string:

0: 'best'


1: 'upper right'


2: 'upper left'


3: 'lower left'


4: 'lower right'


5: 'right'


6: 'center left'


7: 'center right'


8: 'lower center'


9: ' upper center'


10: 'center'


3. Line style

(1) Color

The keyword parameter color (or c) of the plot method is used to set the color of the line. Possible values ​​are:

1, color name or abbreviation


b: blue


g: green


r: red


c: cyan


m: magenta


y: yellow


k: black


w: white


2、#rrggbb


3、(r, g, b) or (r, g, b, a), where r g b a takes the string form of floating point numbers between [0, 1]


4 and [0, 1], representing grayscale values. 0 means black, 1 means white


(2) Style

The keyword parameter linestyle (or ls) of the plot method is used to set the line style. Possible values ​​are:

  • -, solid

  • --, dashed

  • -. , dashdot

  • :, ​​dotted

  • '', ' ', None

  • ##(3 )Thickness

Set the keyword parameter linewidth (or lw) of the plot method to change the thickness of the line, and its value is a floating point number.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, c='r', ls='--', lw=3)
plt.plot(x, y2, c='#526922', ls='-.')
plt.show()
Copy after login

python tutorial on using matplotlib to draw a line chart
##4. marker

The following keyword parameters Can be used to set the marker style:

marker
  • markeredgecolor or mec
  • markeredgewidth or mew
  • ##markerfacecolor or mfc

  • markerfacecoloralt or mfcalt

  • markersize or ms

  • The possible values ​​of marker are:

'.': point marker

  • ',': pixel marker

  • 'o': circle marker

  • 'v': triangle_down marker

  • ##' ^': triangle_up marker

  • '<': triangle_left marker

  • '>': triangle_right marker

  • '1': tri_down marker

  • '2': tri_up marker

  • '3': tri_left marker

  • '4': tri_right marker

  • 's': square marker

  • 'p': pentagon marker

  • '*': star marker

  • 'h': hexagon1 marker

  • 'H': hexagon2 marker

  • '+': plus marker

  • 'x': x marker

  • 'D': diamond marker

  • 'd': thin_diamond marker

  • '|': vline marker

  • '_': hline marker

例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, marker=&#39;o&#39;, mec=&#39;r&#39;, mfc=&#39;w&#39;)
plt.plot(x, y2, marker=&#39;*&#39;, ms=10)
plt.show()
Copy after login

python tutorial on using matplotlib to draw a line chart

另外,marker关键字参数可以和color以及linestyle这两个关键字参数合并为一个字符串。例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, &#39;ro-&#39;)
plt.plot(x, y2, &#39;g*:&#39;, ms=10)
plt.show()
Copy after login

python tutorial on using matplotlib to draw a line chart

更多python tutorial on using matplotlib to draw a line chart相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template