程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- data visualization using Matplotlib Library

編輯:Python

One 、 Data visualization

  • Data visualization is the presentation of data about graphs or tables . With the help of graphic means , Convey and communicate information clearly and effectively . There are studies that show that , The human brain receives or understands pictures faster than words 6 ten thousandfold , So no matter how neat the data is , What a good form , It is not as simple as a picture 、 quick .
    In data analysis , Data visualization is a very important part . Data visualization is not only to show the results of data analysis , And more importantly, data visualization is used for data analysis , For example, abnormal values in data samples are found , Observe the distribution of data , Looking for correlations between data, etc .
    stay Python Data visualization tools in , Data visualization with good applicability has Matplotlip、Seaborn、pyecharts=Python + Echarts,Echarts A pure Javascript Chart library, etc . among ,Matplotlip Is a drawing tool library for creating publishing quality charts ,Seaborn Is in Matplotlib On the basis of this, a more advanced API encapsulation , So it's easier to draw .

Two 、matplotlib Drawing process

3、 ... and 、 Create drawing objects —figure object

  • Before drawing , First create the drawing object . If you call the drawing directly without creating plot function , Matplotlib Will automatically create a drawing object .
    Function library for importing drawings , namely pyplot Sub Library . Its code :
    import matplotlib.pyplot as plt
    Create a blank canvas , You can specify the canvas size , Pixels . Its code :
    plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
    Parameter description :
    num: Image number or name , Numbers are numbers , The string is the name
    figsize: Appoint figure Width and height , In inches ;
    dpi Parameter specifies the resolution of the drawing object , How many pixels per inch , The default value is 80 1 Inches equals 2.5cm,A4 Paper is 21*30cm Of paper
    facecolor: The background color
    edgecolor: Border color
    frameon: Show border or not
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
# Randomly generated 【0,10】 Between 1000 Of an arithmetic sequence x Value .
y = 3*x +1
plt.figure(figsize=(8,4),facecolor=('Pink'),edgecolor='blue')
plt.plot(x,y,color="red",linewidth=3,linestyle='--')
plt.show()

Four 、 draw sin(x) and cos(x) Graph (plot)

  • matplotlib.pyplot Medium plot() Function to draw a line graph .
    plot() Common formats for functions :
    plot(x Axis ,y Axis , Broken line shape color mark , The label is shown in the legend )
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4*np.pi)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,color="red",linestyle='-',linewidth=3)
plt.plot(x,z,color="blue",linestyle='--',linewidth=3)
plt.show()

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4*np.pi)
y = np.sin(x)
z = np.cos(x)
plt.figure(figsize=(10,4))
plt.subplot(121)
plt.plot(x,y,color="red",linestyle='-',linewidth=3)
plt.subplot(122)
plt.plot(x,z,color="blue",linestyle='--',linewidth=3)
plt.show()

5、 ... and 、 Create subgraphs

stay Matplotlib in , A drawing object can be divided into several drawing areas , Different images can be drawn in each drawing area , This form of drawing is called creating subgraphs .
Create subgraphs : Use subplot() function , The syntax format of this function :
plt.subplot(numRows,numCols,plotNum)

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-10,10)
b1=np.sin(x)
c1=np.cos(x)
d1=2*x+4
e1=x**2+2*x+1
plt.figure(figsize=(8,4))
# Create drawing objects 
plt.subplot(2,2,1)
# Equivalent to plt.subplot(221)
plt.title('sin function ')
plt.plot(a1,b1)
plt.subplot(2,2,2)
plt.title('cos function ')
plt.plot(a1,c1)
plt.subplot(223)
plt.title(' A straight line ')
plt.plot(a1,d1)
plt.subplot(224)
plt.title(' Quadratic function ')
plt.plot(a1, e1)
plt.tight_layout(3,3,3)
plt.show()

6、 ... and 、matplotlib Parameter configuration

When drawing figures , There are many properties to configure , Such as color 、 typeface 、 Line type, etc .Matplotlib Save the default configuration in “matplotlibrc” In profile , By modifying the profile , You can change the default style of the chart , This is called a rc Configuration or rc Parameters .
stay Matplotlib You can use more than one “matplotlibrc” The configuration file , Their search order :
1. current path 2. User configuration path 3. System configuration path
(1) modify rcParams A variable's value : Using Matplotlib When drawing , Sometimes, some symbols are involved in the annotation in the drawing , Especially Chinese , Negative signs, etc. may not be displayed normally .

plt.rcParams['font.family']=['SimHei']
# Show the Chinese characters in the figure 
plt.rcParams['axes.unicode_minus']=False
# Show the minus sign in the figure 
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-10,10)
b1=np.sin(x)
c1=np.cos(x)
d1=2*x+4
e1=x**2+2*x+1
plt.figure(figsize=(8,4)) # Create a canvas 
plt.suptitle(' Different function diagrams ',fontsize=14) # Displays the overall title of all subgraphs 
plt.subplot(2,2,1) # plt.subplot(221)
plt.title('sin function ')
plt.plot(a1,b1)
plt.subplot(2,2,2)
plt.title('cos function ')
plt.plot(a1,c1)
plt.subplot(223)
plt.title(' A straight line ')
plt.plot(a1,d1)
plt.subplot(224)
plt.title(' Quadratic function ')
plt.plot(a1, e1)
plt.tight_layout(3,3,3)
plt.show()

(2) Use rc Function to modify parameter configuration :
Use rc Functions can be modified “matplotlibrc” Parameters in the configuration file ,rc Syntax of functions :
matplotlib.rc(group,**kwargs)
Modified the configuration file , You can call rcdefaults(), Restore to the default configuration . matplotlib.rcdefaults()
To reload the latest configuration file , You can call update().matplotlib.rcParams.update(matplotlib.rc_params())

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("lines", marker="H",markersize=17,markerfacecolor='b',linewidth=3,linestyle='-.')
font = {
'family' : 'monospace',
'style' : 'italic',
'size' : '16'}
matplotlib.rc("font", **font)
plt.title("rc() Example")
plt.plot([1,2,3],[3,4,5],color='r')
plt.show()

7、 ... and 、matplotlib Mapping ---- Add the title 、 Set the scale and range

matplotlib.pyplot Add graph title , Axis name , Set the scale and range , There is no order .

8、 ... and 、 Broken line diagram

  • The line chart is more suitable for describing and comparing the trend of multiple groups of data over time . Or the dependence of one set of data on another set of data . Or look at the dependent variable y With the independent variable x Changing trends .
    Usage method :
    matplotlib.pyplot Medium plot() function .
    plt.plot(x,y,format_string,**kwargs)
    Relevant parameters can be set :
    The position of the upper end of the line chart , Mark the shape of the symbol , Size and color and the color of the lines , Linetype and other styles .

Nine 、 Example of line chart

As attached 1 Shown :
analysis 1996~2015 Annual population changes .
file populations.csv It contains 1996~2015 Of the population , View each feature ( Total Population 、 Male population , Female population 、 Urban population and rural population ) Over time ( year ) Changes that occur over time .

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = 'SimHei'
## Set Chinese display 
matplotlib.rcParams['axes.unicode_minus'] = False
# Read data by column 
a0 = np.loadtxt('populations.csv',delimiter=',',dtype=np.str,
usecols=(0,),skiprows=1,unpack=False)
a1,a2,a3,a4,a5=np.loadtxt('populations.csv',delimiter=',',dtype=np.float,
usecols=(1,2,3,4,5),skiprows=1,unpack=True)
header = [' Total Population ',' Male population ',' Female population ',' Urban population ',' The rural population ']
# Data collation 
a0,a1,a2,a3,a4,a5=a0[::-1],a1[::-1],a2[::-1],a3[::-1],a4[::-1],a5[::-1]
plt.figure(figsize=(12,6)) # Set the size of the drawing object ( canvas )
plt.xlabel(' Time - year ') # Set up x The title of the axis 
plt.ylabel(' The number ') # Set up y The title of the axis 
plt.title('1996--2015 Demographic tables ')
plt.plot(a0,a1,'r-',
a0,a2,'c--',
a0,a3,'-.',
a0,a4,':',
a0,a5,'--',linewidth='2',)
plt.legend(header) # Set legend 
plt.show()

Ten 、 Legend settings

legend(title,loc,fontsize,facecolor,edgecolor,bbox_to_anchor)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = 'SimHei'## Set Chinese display 
matplotlib.rcParams['axes.unicode_minus'] = False
# Read data by column 
a0 = np.loadtxt('populations.csv',delimiter=',',dtype=np.str,
usecols=(0,),skiprows=1,unpack=False)
a1,a2,a3,a4,a5=np.loadtxt('populations.csv',delimiter=',',dtype=np.float,
usecols=(1,2,3,4,5),skiprows=1,unpack=True)
header = [' Total Population ',' Male population ',' Female population ',' Urban population ',' The rural population ']
# Data collation 
a0,a1,a2,a3,a4,a5=a0[::-1],a1[::-1],a2[::-1],a3[::-1],a4[::-1],a5[::-1]
plt.figure(figsize=(11,5)) # Set the size of the drawing object ( canvas )
plt.xlabel(' Time - year ') # Set up x The title of the axis 
plt.ylabel(' The number ') # Set up y The title of the axis 
plt.title('1996--2015 Demographic tables ')
plt.plot(a0,a1,
a0,a2,
a0,a3,
a0,a4,
a0,a5,'--',linewidth='2',)
plt.legend(header,loc=1)
plt.show()

11、 ... and 、 Save and display diagram

Twelve 、 Practice

As attached 2 Shown :
Use different colors 、 Different line styles , draw 2007–2016 National employment in , Broken line chart of urban and rural employees (Employedpopulation.csv).
among , Employed persons nationwide ( ten thousand people ) Use solid red lines '-‘ Express , Urban employees ( ten thousand people ) Use a long green dotted line ’–' Express , Rural employees ( ten thousand people ) With blue dotted lines '-.' Express .

import numpy as np
import matplotlib.pyplot as plt
# Set up matplotlib Normal display of Chinese and minus sign 
plt.rcParams['font.sans-serif']=['SimHei'] # Show Chinese in bold 
plt.rcParams['axes.unicode_minus']=False # The minus sign is displayed normally 
# Import data 
Emp_data= np.loadtxt('Employedpopulation.csv',delimiter = ",",
usecols=(1,2,3,4,5,6,7,8,9,10),dtype=int)
# Create a drawing object , And set the width and height of the object 
plt.figure(figsize=(8, 4))
plt.xlabel(' year ')
plt.ylabel(' personnel ( ten thousand people )')
year = [i for i in range(2007,2017)]
plt.ylim((30000,80000)) # Set up y Axis range 
plt.xticks(year,year) # Set the scale 
plt.title("2007-2016 Year town 、 Line chart of the situation of rural and all employed persons ")
# Draw the wiring diagram of employed persons 
plt.plot(Emp_data[0],Emp_data[1],"r-",
Emp_data[0],Emp_data[2],"g--",
Emp_data[0],Emp_data[3],"b-.")
# Add legend 
plt.legend([' Full employment ',' Urban employment ',' Rural employment '])
plt.savefig(' Line chart of employed persons .png')
plt.show()

13、 ... and 、 Scatter plot

  • A scatter chart uses coordinate points ( Scatter ) A graph that reflects the statistical relationship between features ( Distribution pattern of coordinate points ).
    pyplot.scatter(x, y, s=None, c=None, marker=None, alpha=None)
    Common parameters and descriptions :
    x,y: A pair of data , It can also be accepted array, Express x Axis and y Data corresponding to the axis .
    s: Specify the size of the point . Receive numeric or one-dimensional array, If you pass in one dimension array Represents the size of each point . The default is None
    c: Specify the color of the point . Receive color or one-dimensional array, If you pass in one dimension array Represents the color of each point . The default is None
    marker: The shape of the dot . Receive specific string.
    alpha: transparency

  • Three months after the opening of a shopping mall , Some customers reported that the mobile phone signal at some locations on the first floor of the shopping mall was poor , Sometimes, some cashiers cannot normally use wechat payment or Alipay , There are some places in the mall where wechat cannot be used normally . So , The mall arranges staff to test the signal strength of mobile phones at different locations in order to further improve the service quality and user experience , The test data is saved in a file “ Signal strength of mobile phones in shopping malls .txt” in . Three numbers separated by commas are used in each line of the document to represent a location in the shopping mall x、y Coordinates and signal strength , among x、y The coordinate value takes the southwest corner of the shopping mall as the coordinate origin and eastward as x Positive axis ( common 150 rice )、 To the north is y Positive axis ( common 30 rice ), The signal strength is measured in 0 No signal 、100 Indicates the strongest .

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']='simhei'
# Load data 
data = np.loadtxt(' Signal strength of mobile phones in shopping malls .txt',dtype=np.int,delimiter=',')
plt.title(' Signal strength of mobile phones in shopping malls ',fontsize=16)
plt.ylabel(' In the west \n south \n horn \n towards \n north ',rotation=0,labelpad=16)
plt.xlabel(' Southwest corner to East ')
x_data = data[:,0] # x Coordinate data 
y_data = data[:,1] # y Coordinate data 
s_data = data[:,2] # Signal strength data 
# Draw a scatter plot 
plt.scatter(x_data,y_data,s=s_data,marker='*',c ='b',alpha=0.4)
#plt.grid(True) # Show gridlines 
plt.show()


If attachments are required 1 and 2 Do exercises , Please add an article editor QQ:2122961493 receive .

above , About Python Language adoption matplotlib Library for data visualization , Have you learned ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved