Road python learning - visualization tool matplotlib (on)

Previous issues have already read and write data, data preprocessing introduction is over, then we introduce today a visual library matplotlib, although now has more advanced visualization library, such as seaborn, ploty, pyecharts, etc., but the most matplotlib after the foundation, the most comprehensive mapping idea visualization library, learned matplotlib, to learn the other becomes easier.

1. Before mapping

In order to show that the normal mapping, mapping often need to be added prior to the following code:

#解决中文乱码的问题
plt.rcParams["font.sans-serif"] = "SimHei"

#解决负号不能正常显示的问题
plt.rcParams["axes.unicode_minus"] = False

#直接在jupyter notebook展示图片
%matplotlib inline

#作图使用svg格式更为清晰
%config InlineBackend.figure_format = "svg"

1.1 Visualization pseudocode

设置画布的大小:plt.figure(figsize = (x,y))
建立坐标系:     add_subplot()、plt.subplot()、plt.subplots()
作图函数:       plot、bar、scatter、barh、pie、boxplot 
设置图表标题:   plt.title()
设置x轴y轴标题: plt.x/ylabel()
设置x轴y轴刻度: plt.x/yticks()
设置x轴y轴范围: plt.x/ylims()
添加数据标签:   plt.text()
添加注释:       plt.annotate()
添加数据表格:   plt.table()
添加图例:       plt.legend()
展示结果:       plt.show()

1.2 establish the coordinate system

Method of establishing the coordinate system there are several, add_subplot () is an object-oriented approach, that is, you must first establish the canvas and coordinate system is established on the canvas before mapping on the coordinate system; the latter two (plt.subplot (), plt.subplots ()) belonging function approach, which is direct call function plt established coordinate system, except that plt.subplot () once established only a few designated coordinates, and plt.subplots () is Full-time coordinate system are built up, such as:

# add_subplot(行,列,第几个)建立坐标系
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

#plt.subplot(行,列,第几个)建立坐标系
plt.subplot(2,2,1)
plt.subplot(2,2,4)

#plt.subplots(行,列)建立坐标系
plt.subplots(1,2)

1.3 mapping function

Mapping functions include many commonly used Plot line chart, bar chart bar, Barh bar, pie PIE, Scatter scattergram, an area chart stackplot, FIG Polar radar, thermal FIG imshow like, is not specifically described here first , etc. after introduction to other parameters described examples.

1.4 The title of the chart

The title graph by plt.title () is provided in the following format:

plt.title("xxxx",loc = "left/right/center",fontsize = x,color = x,fontwidth = x)

The coordinate axis setting 1.5

(1) Title of the shaft is provided: plt.xlabel / ylabel ( "xxxx", labelpad = x, fontsize = x, color = x, fontwidth = x)

(2) the range of the coordinate system: plt.xlim / ylim (x, y)

(3) 坐标轴的刻度:plt.xticks/yticks(range,labels = [])

1.6 附加元素

(1) 网格线设置:plt.grid(True,axis = "x/y",linestyle = x,linewidth = x);

不设定axes时,默认xy轴的网格线都打开;

linestyle表示线的风格,有实线(solid),虚线(dashed),线点(dashdot),虚点线(dotted)。

(2)数据标签的设置:plt.text(x,y,z,ha = "left/right/center",va = "top/bottom/center", fontsize = s,color = s);

ha表示水平的位置,va表示垂直的位置;

格式如下:

for a,b in zip(x,y):
      plt.text(a,b,b,ha = xx,va = xx)

(3)注释:plt.annotate("xxx",xy = (x,y),xytext = (x,y),arrowprops = dict(arrowstyle = "x",facecolor = x));

arrowstyle表示注释的风格,常用的有"->","-]","-","<-","<->","fancy"(头小尾大的箭头);

xy表示需要注释的位置,而xytext表示注释文本所在的位置。

(4)数据表格:plt.table(cellText,cellColours,cellLoc,rowLabels,rowColours,rowLoc,colLabels,colColours,colLoc,loc);

数据表格包含三要素,分别是数值,行和列,数值通过cellText设置,行列标签通过rowLabels和colLabels设置;

需要注意的是数值、行、列的颜色填充分别是cellColours,rowColours和colColours,若写成cellColors,程序会报错。

(5)图例的设置:plt.legend(ncol = x,loc = x);

ncol表示图列分为几列来显示,默认是一列。

1.7 折线图

折线图的格式如下:

plt.plot(x,y,linestyle = s,linewidth = s,color = s,marker = s,markersize = s,markeredgecolor = s, markeredgewidth = s,markerfacecolor = s,label);

marker表示点的风格:常用的有"+","o","*","s"(正方形),"p"(五边形),"h"(六边形),"d"(小菱形);

常用的颜色有:"b"(蓝色),"g"(绿色),"k"(黑色),"w"(白色),"r"(红色),"y"(黄色),"c"(青色),"m"(品红)。

import numpy as np
plt.subplot(1,1,1)
x = np.arange(10)
y = np.arange(10)
plt.plot(x,y,
         linestyle = "dashdot",
         linewidth = 2,
         color = "c",
         marker = "*",
         markersize = 10,
         markerfacecolor = "m",
         markeredgecolor = "m",
         label = "示例1")
plt.title("示例1",fontsize = 15,color = "k",loc = "center")
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.grid(b = True,axis = "x",linestyle = "dashed")
for a,b in zip(x,y):
    plt.text(a,b-0.5,b,va = "center",ha = "center")
plt.annotate("这是坐标xy都是5的位置",xy = (5,5),xytext=(6,4),arrowprops = dict(arrowstyle = "fancy",facecolor = "r"))
plt.legend()
<matplotlib.legend.Legend at 0x19fccb46f60>

 

1.8 柱形图与条形图

柱形图与条形图的原理相似,只是height跟width两个参数含义交换,这里只介绍柱形图。

柱形图的格式如下:plt.bar(x,height = s,width = s,color = s,edgecolor = s,align = center/edge);

align表示柱子与x的关系,center表示柱子位于x轴的中心,edge表示柱子位于x轴的边缘;

height表示柱子的高度,就是y值;

width表示柱子的宽度。

import numpy as np
x = np.arange(1,5)
y1 = np.arange(1,5)
y2 = np.arange(2,6)
plt.subplot(2,2,1)
plt.bar(x,
        height = y1,
        width = 0.3,
        align = "center",
        facecolor = "c")
for a,b in zip(x,y1):
    plt.text(a,b,b,va = "center",ha = "center")

#簇状柱形图
plt.subplot(2,2,2)
plt.bar(x,
        height = y1,
        width = 0.3,
        align = "center",
        facecolor = "c",
        label = "示例1")
plt.bar(x+0.3,
        height = y2,
        width = 0.3,
        align = "center",
        facecolor = "m",
        label = "示例2")
for a,b in zip(x,y1):
    plt.text(a,b,b,va = "center",ha = "center")
for a,b in zip(x,y2):
    plt.text(a+0.3,b,b,va = "center",ha = "center")
plt.legend()

#堆积柱形图
plt.subplot(2,2,3)
plt.bar(x,
        height = y1,
        width = 0.3,
        align = "center",
        facecolor = "c",
        label = "示例1")
plt.bar(x,
        height = y2,
        width = 0.3,
        align = "center",
        facecolor = "m",
        label = "示例2")
for a,b in zip(x,y1):
    plt.text(a,b,b,va = "center",ha = "center")
for a,b in zip(x,y2):
    plt.text(a,b,b,va = "center",ha = "center")
plt.legend()

#堆积柱形图
plt.subplot(2,2,4)
plt.bar(x,
        height = y2,
        width = 0.3,
        align = "center",
        facecolor = "m",
        label = "示例2")
plt.bar(x,
        height = y1,
        width = 0.3,
        align = "center",
        facecolor = "c",
        label = "示例1")
for a,b in zip(x,y1):
    plt.text(a,b,b,va = "bottom",ha = "center")
for a,b in zip(x,y2):
    plt.text(a,b,b,va = "top",ha = "center")
plt.legend()
<matplotlib.legend.Legend at 0x19fce42c5c0>

簇状柱形图的原理就是在同样的x值做不同的柱子时,第二个柱子往左或者往右移动一个柱子的宽度,如上图所示的0.3;

堆积柱形图的原理是只要是在相同的x值作不同的柱子时,柱子会自动叠加,这里需要注意的是更大的y值需要先作图,不然的话会像图3所示,正确的做法如图四所示,y2的值更大,所以先作图。

1.9 饼图

饼图的格式如下:plt.pie(x,explode = [],labels = [],autopct = s,pctdistance = s,radius = s,labeldistance = s, shadow = True/False,wedgeprops = dict(width = s,edgecolor = s),center = s, counterclock = True/False)

explode表示饼图中每一块离圆心的位置;

autopct表示数据标签中数值的百分比格式;

pctdistance表示数据标签距离圆心的距离;

radius表示饼图的半径;

labeldistance表示每一块图例离圆心的距离;

shadow表示是否有阴影;

wedgeprops表示每一块的边界属性;

center表示圆心的位置;

counterclock表示是否让饼图逆时针显示。

#饼图
plt.subplot(1,2,1)
x = [i**2 for i in np.arange(1,5)]
plt.pie(x,
       explode= [0,0,0.2,0],
       labels = ["东","南","西","北"],
       colors = ["b","steelblue","c","g"],
       autopct = "%.1f%%",
       radius = 1,
       pctdistance= 0.7,
       labeldistance = 0.5,
       shadow= True,
       counterclock = False)

#圆环图
plt.subplot(1,2,2)
x = [i**2 for i in np.arange(1,5)]
plt.pie(x,
       labels = ["东","南","西","北"],
       radius = 1,
       labeldistance = 1.2,
       wedgeprops= dict(width = 0.3,edgecolor = "w"))
plt.pie(x,
       radius = 0.8,
       wedgeprops= dict(width = 0.3,edgecolor = "w"))

圆环图的原理就是设置不同的半径,然后通过设置边界的宽度与颜色来达到分离的效果。

1.10 散点图

散点图的格式如下:plt.scatter(x,y,marker = a,s = a,c = a,edgecolors = a,linewidths = a)

s表示散点的大小;

c表示散点的颜色;

edgecolors表示散点边缘的颜色;

linewidths表示散点的线宽。

# 散点图
x = np.arange(1,5)
y = [i**2 for i in x]
plt.subplot(1,2,1)
plt.scatter(x,
            y,
           marker = "d",
           s = 200,
           c = "c",
           edgecolors = "m")
for a,b in zip(x,y):
    plt.text(a,b,b,ha = "center",va = "center")

#气泡图
plt.subplot(1,2,2)
plt.scatter(x,
            y,
           marker = "o",
           s = y*200,         #根据y值设置散点的大小
           c = y)             #根据y值生成不同的颜色
for a,b in zip(x,y):
    plt.text(a,b,b,ha = "center",va = "center")

发布了33 篇原创文章 · 获赞 30 · 访问量 3万+

Guess you like

Origin blog.csdn.net/d345389812/article/details/89073990