[Turn] Python--matplotlib drawing visualization knowledge points finishing

reference

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
labels='frogs','hogs','dogs','logs'
sizes=15,20,45,10
colors='yellowgreen','gold','lightskyblue','lightcoral'
explode=0,0.1,0,0
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f%%',shadow=True,startangle=50)
plt.axis('equal')
plt.show()

matplotlib icon displays Chinese normally

In order to be able to display Chinese and negative signs in the chart, the following settings are required:

1
2
3
import matplotlib.pyplot as plt 
plt.rcParams['font.sas-serig']=['SimHei'] #Used to display Chinese labels 
normally plt.rcParams['axes.unicode_minus']=False #Used to display negative signs normally

 

matplotlib inline和pylab inline

You can use to ipython --pylabopen the ipython naming window.

1
2
%matplotlib inline #notebook mode 
%pylab inline #ipython mode

Both of these commands can embed the picture in the interactive window when drawing, instead of popping up a picture window. However, there is a flaw: unless the code is executed once, the drawing cannot be superimposed, because in these two modes , Is to pltappear, the picture will appear immediately show, so:

It is recommended to use it in ipython notebook, so that you can easily edit the code and draw at one time.

Set matplotlib parameters for the project

During code execution, there are two ways to change parameters:

  • Use parameter dictionary (rcParams)
  • Call the matplotlib.rc() command to modify the parameters by passing in the keyword meta-ancestor

If you don't want to configure in the code section every time you use matplotlib, you can modify the file parameters of matplotlib. You can use matplot.get_config()commands to find the current user's configuration file directory.

The configuration file includes the following configuration items:

axex: set the color of the coordinate axis boundary and surface, the size of the coordinate scale and the display of the grid
backend: set the target TkAgg and GTKAgg
figure: control the dpi, border color, graphic size, and subplot settings
font: font set (Font family), font size and style settings
grid: set the grid color and linear
legend: set the display of the legend and the text in it
line: set the line (color, line type, width, etc.) and mark
patch: fill the 2D space Graphic objects such as polygons and circles. Control line width, color and anti-aliasing settings, etc.
savefig: The saved figure can be set individually. For example, set the background of the rendered file to white.
verbose: Set matplotlib to output information during execution, such as silent, helpful, debug, and debug-annoying.
xticks and yticks: Set the color, size, direction, and label size for the major and minor ticks of the x and y axes.

Line-related attribute mark settings

The attributes used for the line of the table

Line style linestyle or ls description Line style linestyle or ls description
‘-‘ solid line ‘:’ dotted line  
‘–’ Dashed line ‘None’,’ ‘,’’ Draw nothing  
‘-.’ Dotted line  

Line mark

Mark maker description mark description
'O' Circle ‘.’ point
‘D’ diamond ‘s’ square
‘h’ Hexagon 1 ‘*’ Asterisk
‘H’ Hexagon 2 ‘d’ Small diamond
‘_’ Horizontal line 'v' Triangle with one corner down
‘8’ Octagon ‘<’ One corner morning left triangle
‘p’ Pentagon ‘>’ Triangle with one corner to the right
‘,’ Pixel ‘^’ Triangle with one corner up
‘+’ plus ‘\ Vertical line
‘None’,’’,’ ‘ no ‘x’ X

colour

You can matplotlib.pyplot.colors()get all the colors supported by matplotlib by calling .

Alias colour Alias colour
b blue g green
r red and yellow
c Blue k black  
m Magenta w white

If these two colors are not enough, there are two other ways to define the color value:

  • Use HTML hexadecimal strings to  color='eeefff' use valid HTML color names ('red','chartreuse', etc.).
  • You can also pass in an RGB ancestor normalized to [0,1]. color=(0.3,0.3,0.4)

Many methods can introduce color parameters, such as title().
plt.tilte('Title in a custom color',color='#123456')

Background color

By providing a parameter to methods such as matplotlib.pyplot.axes()or , you can specify the background color of the coordinates.matplotlib.pyplot.subplot()axisbg

subplot(111,axisbg=(0.1843,0.3098,0.3098)

basis

If you provide a one-dimensional array or list to the plot() command, matplotlib will default it to a series of y values ​​and automatically generate x values ​​for you. The default x vector starts from 0 and has the same length as y, so the data of x is [0,1,2,3].

Determine the coordinate range

  • plt.axis([xmin, xmax, ymin, ymax])
    The axis() command in the above example specifies the coordinate range.
  • xlim(xmin, xmax) and ylim(ymin, ymax) to adjust the x, y coordinate range
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    %matplotlib inline 
    import numpy as np 
    import matplotlib.pyplot as plt 
    from pylab import * 
    
    x = np.arange(-5.0, 5.0, 0.02) 
    y1 = np.sin(x) 
    
    plt.figure(1) 
    plt.subplot(211) 
    plt.plot(x, y1) 
    
    plt.subplot(212 ) #Set the 
    x-axis range 
    xlim(-2.5, 2.5) 
    #Set the 
    y-axis range ylim(-1, 1) 
    plt.plot(x, y1)
    

Overlay

Use one command to draw multiple lines of different formats.

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

 

plt.figure()

你可以多次使用figure命令来产生多个图,其中,图片号按顺序增加。这里,要注意一个概念当前图和当前坐标。所有绘图操作仅对当前图和当前坐标有效。通常,你并不需要考虑这些事,下面的这个例子为大家演示这一细节。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plt
plt.figure(1)                # 第一张图
plt.subplot(211)             # 第一张图中的第一张子图
plt.plot([1,2,3])
plt.subplot(212)             # 第一张图中的第二张子图
plt.plot([4,5,6])


plt.figure(2)                # 第二张图
plt.plot([4,5,6])            # 默认创建子图subplot(111)

plt.figure(1)                # 切换到figure 1 ; 子图subplot(212)仍旧是当前图
plt.subplot(211)             # 令子图subplot(211)成为figure1的当前图
plt.title('Easy as 1,2,3')   # 添加subplot 211 的标题

 

figure感觉就是给图像ID,之后可以索引定位到它。

plt.text()添加文字说明

  • text()可以在图中的任意位置添加文字,并支持LaTex语法
  • xlable(), ylable()用于添加x轴和y轴标签
  • title()用于添加图的题目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# 数据的直方图
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)


plt.xlabel('Smarts')
plt.ylabel('Probability')
#添加标题
plt.title('Histogram of IQ')
#添加文字
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()


text中前两个参数感觉应该是文本出现的坐标位置。

plt.annotate()文本注释

在数据可视化的过程中,图片中的文字经常被用来注释图中的一些特征。使用annotate()方法可以很方便地添加此类注释。在使用annotate时,要考虑两个点的坐标:被注释的地方xy(x, y)和插入文本的地方xytext(x, y)。[^1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import matplotlib.pyplot as plt

ax = plt.subplot(111)

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)

plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
            arrowprops=dict(facecolor='black', shrink=0.05),
            )

plt.ylim(-2,2)
plt.show()


[^1]:DataHub-Python 数据可视化入门1

plt.xticks()/plt.yticks()设置轴记号

现在是明白干嘛用的了,就是人为设置坐标轴的刻度显示的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 导入 matplotlib 的所有内容(nympy 可以用 np 这个名字来使用)
from pylab import *

# 创建一个 8 * 6 点(point)的图,并设置分辨率为 80
figure(figsize=(8,6), dpi=80)

# 创建一个新的 1 * 1 的子图,接下来的图样绘制在其中的第 1 块(也是唯一的一块)
subplot(1,1,1)

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

# 绘制余弦曲线,使用蓝色的、连续的、宽度为 1 (像素)的线条
plot(X, C, color="blue", linewidth=1.0, linestyle="-")

# 绘制正弦曲线,使用绿色的、连续的、宽度为 1 (像素)的线条
plot(X, S, color="r", lw=4.0, linestyle="-")

plt.axis([-4,4,-1.2,1.2])
# 设置轴记号

xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
       [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

yticks([-1, 0, +1],
       [r'$-1$', r'$0$', r'$+1$'])
# 在屏幕上显示
show()


当我们设置记号的时候,我们可以同时设置记号的标签。注意这里使用了 LaTeX。[^2]

[^2]:Matplotlib 教程

移动脊柱 坐标系

1
2
3
4
5
6
7
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

这个地方确实没看懂,囧,以后再说吧,感觉就是移动了坐标轴的位置。

plt.legend()添加图例

1
2
3
4
plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plot(X, S, color="red",  linewidth=2.5, linestyle="-", label="sine")

legend(loc='upper left')

matplotlib.pyplot

使用plt.style.use('ggplot')命令,可以作出ggplot风格的图片。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Import necessary packages
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from sklearn import datasets
from sklearn import linear_model
import numpy as np
# Load data
boston = datasets.load_boston()
yb = boston.target.reshape(-1, 1)
Xb = boston['data'][:,5].reshape(-1, 1)
# Plot data
plt.scatter(Xb,yb)
plt.ylabel('value of house /1000 ($)')
plt.xlabel('number of rooms')
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit( Xb, yb)
# Plot outputs
plt.scatter(Xb, yb,  color='black')
plt.plot(Xb, regr.predict(Xb), color='blue',
         linewidth=3)
plt.show()

给特殊点做注释

好吧,又是注释,多个例子参考一下!

我们希望在 2π/32π/3 的位置给两条函数曲线加上一个注释。首先,我们在对应的函数图像位置上画一个点;然后,向横轴引一条垂线,以虚线标记;最后,写上标签。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
t = 2*np.pi/3
# 作一条垂直于x轴的线段,由数学知识可知,横坐标一致的两个点就在垂直于坐标轴的直线上了。这两个点是起始点。
plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--")
scatter([t,],[np.cos(t),], 50, color ='blue')

annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
         xy=(t, np.sin(t)), xycoords='data',
         xytext=(+10, +30), textcoords='offset points', fontsize=16,
         arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--")
scatter([t,],[np.sin(t),], 50, color ='red')

annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
         xy=(t, np.cos(t)), xycoords='data',
         xytext=(-90, -50), textcoords='offset points', fontsize=16,
         arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

 

plt.subplot()

plt.subplot(2,3,1)表示把图标分割成2*3的网格。也可以简写plt.subplot(231)。其中,第一个参数是行数,第二个参数是列数,第三个参数表示图形的标号。

plt.axes()

我们先来看什么是Figure和Axes对象。在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个,或者多个Axes对象。每个Axes对象都是一个拥有自己坐标系统的绘图区域。其逻辑关系如下^3

plt.axes-官方文档

  • axes() by itself creates a default full subplot(111) window axis.
  • axes(rect, axisbg=’w’) where rect = [left, bottom, width, height] in normalized (0, 1) units. axisbg is the background color for the axis, default white.
  • axes(h) where h is an axes instance makes h the current axis. An Axes instance is returned.

    rect=[左, 下, 宽, 高] 规定的矩形区域,rect矩形简写,这里的数值都是以figure大小为比例,因此,若是要两个axes并排显示,那么axes[2]的左=axes[1].左+axes[1].宽,这样axes[2]才不会和axes[1]重叠。

show code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
http://matplotlib.org/examples/pylab_examples/axes_demo.html

import matplotlib.pyplot as plt
import numpy as np

# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000]/0.05)               # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt  # colored noise

# the main axes is subplot(111) by default
plt.plot(t, s)
plt.axis([0, 1, 1.1*np.amin(s), 2*np.amax(s)])
plt.xlabel('time (s)')
plt.ylabel('current (nA)')
plt.title('Gaussian colored noise')

# this is an inset axes over the main axes
a = plt.axes([.65, .6, .2, .2], axisbg='y')
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title('Probability')
plt.xticks([])
plt.yticks([])

# this is another inset axes over the main axes
a = plt.axes([0.2, 0.6, .2, .2], axisbg='y')
plt.plot(t[:len(r)], r)
plt.title('Impulse response')
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])

plt.show()

 

[^3]:绘图: matplotlib核心剖析

pyplot.pie参数

colors颜色

找出matpltlib.pyplot.plot中的colors可以取哪些值?

打印颜色值和对应的RGB值。

  • plt.axis('equal')避免比例压缩为椭圆

autopct

  • How do I use matplotlib autopct?
    1
    
    autopct enables you to display the percent value using Python string formatting. For example, if autopct='%.2f', then for each pie wedge, the format string is '%.2f' and the numerical percent value for that wedge is pct, so the wedge label is set to the string '%.2f'%pct.

Guess you like

Origin blog.csdn.net/weixin_52071682/article/details/112210859