python matplotlib use in (a)

Libraries need to import matplotlib

import matplotlib.pyplot as plt

or

from matplotlib.pyplot import *

1, the establishment of a blank map

fig = plt.figure(figsize=(4,2))  
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
plt.show()

In subplot()function of three numbers, the first number represents the sub FIG y-axis direction, x-axis represents the number of the second directional sub graph, and the third to represent the current focus of the drawing.
Here Insert Picture Description
Here can be seen in FIG x, y-axis coordinates are from 0 to 1, we can use to modify the coordinates specify the start value statement:

ax1.axis([-1, 1, -1, 1])

or

plt.axis([-1, 1, -1, 1])

We can also add a title and the horizontal and vertical coordinates of the label to a subgraph:

ax1.set_title("图的名称")
ax1.set_xlabel(u'x轴名称')
ax1.set_ylabel(u'y轴名称')

2, was added to the contents in FIG.

1), Histogram (bar)

fig = plt.figure(figsize=(4,2))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
x = [0,1,2,3,4,5,6,7,8,9,10]
y1 = [0,1,2,3,4,5,6,7,8,9,10]
y2 = [0,1,2,3,4,5,6,7,8,9,10]
y3 = [0,1,2,3,4,5,6,7,8,9,10]
y4 = [0,1,2,3,4,5,6,7,8,9,10]
ax1.bar(x,y1)
ax1.set_title("figure1")
ax2.bar(x,y2)
ax2.set_title("figure2")
ax3.bar(x,y3)
ax3.set_title("figure3")
ax4.bar(x,y4)
ax4.set_title("figure4")
plt.show()

Results are as follows:
Here Insert Picture Description

2), pie chart (PIE)

y = [2, 3, 8.8, 6.6, 7.0]
plt.figure()
plt.pie(y)
plt.title('PIE')
plt.show()

Results are as follows:
Here Insert Picture Description

3), Scatter (scatter)

x = [0,1,2,3,4,5,6,7,8,9,10]
y = [0,1,2,3,4,5,6,7,8,9,10]
plt.scatter(x, y, color='r', marker='+')
plt.show()

Results are as follows:
Here Insert Picture Description
the significance of the parameters:

  1. The abscissa axis x is the amount, y is the vertical coordinate axis vectors, x, y must be the same length.
  2. colorColor control, common colors are as follows:
abbreviation colour
b blue
c cyan
g green
k black
m magenta
r rea
w white
Y yellow
  1. markerControl mark style, popular style:
symbol style
. Point marker
, Pixel marker
O Circle marker
v Triangle down marker
^ Triangle up marker
< Triangle left marker
> Triangle right marker
1 Tripod down marker
2 Tripod up marker
3 Tripod left marker
4 Tripod right marker
s Square marker
p Pentagon marker
* Star marker
h Hexagon marker
H Rotated hexagon D Diamond marker
d Thin diamond marker
_ Horizontal line (hline symbol) marker
+ plus fields
x Cross (x) marker

4), a function of (Plot)

from math import *
from numpy import *
x = arange(-math.pi, math.pi, 0.01)
y = [sin(xx) for xx in x]
plt.figure()
plt.title("sinx")
plt.plot(x, y, color='r', linestyle='-.')
plt.show()

Results are as follows
Here Insert Picture Description
significance parameters:
of the lineStyle is linear control parameters, commonly used are:

symbol Linear
- solid line
Short-term
-. Short white line point
Dotted line

5), two-dimensional graphics

2D

import numpy as np
delta = 0.025
x = y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z = Y**1 + X**2
plt.figure(figsize=(12, 6))
plt.contour(X, Y, Z)
plt.colorbar()
plt.title("2D")
plt.show()

Results are as followsHere Insert Picture Description

Photos read

import matplotlib.image as mpimg
img=mpimg.imread('图片路径')
plt.imshow(img)
plt.title("图片")
plt.show()

ps: if a solution can not be displayed in Figure Chinese:
join in the code:

plt.rcParams['font.sans-serif']=['SimHei']   # 设置中文字体
plt.rcParams['axes.unicode_minus'] = False   # 设置正负号

Reference blog: Reference blog
about more details matplotlib can refer to the official document: matplotlib official documents

Reference Code:

https://github.com/ZhangJiangtao-0108/python in the matplotlib_example.pyfile

Released nine original articles · won praise 2 · Views 100

Guess you like

Origin blog.csdn.net/jocker_775065019/article/details/104819071