0/ Plot in 2 ways

    # matplotlib is the extension package, Pyplot is a module in the extension package, and a.py file
    import matplotlib.pyplot as plt 
   
    #1/ With PLT, initialize the total canvas FIG and subgraph AX lists
    fig,ax = plt.subplots(2.2,figsize=(20.8),dpi=150) 
    ax1 = ax[0.0]
    ax2 = ax[0.1]
      
    #2/ First create the total canvas FIG with PLT, then add subplots to the total canvas by add_subplot()
    fig = plt.figure(figsize=(20.8),dpi=200)  
    ax1 = fig.add_subplot(211,polar=False)   The # polar parameter indicates whether polar coordinates are created. The default is not.
    ax2 = fig.add_subplot(212)
Copy the code

1/ What is Matplotlib

Matplotlib is a Python extension for data visualization. It has a module called PyPlot (modules refer to.py script files) that makes plotting easy by providing control over line styles, font attributes, formatting axes, and more. The main function is to carry out data visualization, drawing all kinds of graphs, such as broken line graph, bar graph, pie chart, scatter chart, quadrant chart, Pareto chart, etc. In addition, there are visual extensions like Pyecharts and Seaborn. No further details are required in this article. Usage: import matplotlib.pyplot as PLTCopy the code

2 / figure/axes/difference between the axis

Figure is the total canvas Axes is the subgraph of the canvas, the area of each drawing. You can have more than one drawing area in a FIG. Axis is the coordinate axis in each subgraph.Copy the code

Figure is the total canvas. Axex is a subgraph of a canvas. A canvas can have one or more subgraphs. Subgraphs are arranged in a matrix. Axis is the coordinate axis, and there are coordinate axes in each subgraph. Each graph is drawn on a canvas and arranged in the form of a matrix to form a matrix subgraph. The subgraph can help us compare the trend of different data more easily.Copy the code

3/ How to draw, how to use matplotlib

<1> Initialize total canvas FIG and subgraph AXES

import matplotlib.pylot as plt

# This is the first step, whatever you do, this is the first step
fig,ax = plt.subplots(2.2,
                      figsize=(30.8),
                      dpi=200,
                      facecolor=None,
                      edgecolor=None,
                      frameon=True)   

The # plt.subplots() function will plot a total canvas FIG and any number of subplots ax
The number of # axes is determined by the first 2 parameters of the function and set according to actual needs. 2,2 refers to 2 rows,2 columns, 4 subgraphs.
The # ax object is a list of all axes subgraphs.
# If the function does not have the first 2 arguments to determine the number of subgraphs, then by default there is only one subgraph axes.

# This function takes the following parameters:
    # figsize: Specifies the width and height of the figure in inches. The total canvas size is determined.
    # dPI: Specifies the resolution of the drawing object, i.e. the number of pixels per inch. The default is 80
    # facecolor: The background color, white by default.
    # edgecolor: Border color, black by default.
    # frameon: Whether to display a border, default is True, and display a border

In addition to the various parameters of the plt.subplots() function, the plt.rcparams command is also available
# such as
  plt.rcParams["figure.dpi"] = 500  # Set the resolution of the graphic
  plt.rcParams["axes.titlesize"] = 20  # Text size of subgraph title
  plt.rcParams['font.sans-serif'] = ['SimHei'] # used to display Chinese labels normally
  plt.rcParams['axes.unicode_minus'] = False Make the axis scale label display negative sign normallyPlease refer to4

# Adjust the distance between the subgraph and the frame of the FIG canvas, as well as the distance between the subgraph and the subgraph
In general, this command can not be written, wait for the actual drawing does not fit, then adjust
# This step can be placed here (and after initializing the canvas and subimage, where adjustments work) or after.plt.subplots_adjust(left=? , bottom=? , right=? , top=? , wspace=? , hspace=?)# left-- distance from graph to left edge;
# bottom-- distance from graph to bottom edge;
# right-- distance from graph to right edge;
# top-- distance from graph to top edge;
# wspace-- the horizontal distance between subgraphs and the distance between left and right subgraphs
# hspace-- the longitudinal distance between subgraphs and the distance between the upper subgraphs

# Set the title for the total canvas FIG
The general header can also have various parameters, such as font, size, color, transparency, etc
# However, there are generally no special requirements
fig.suptitle("General title",fontsize=50,color='black',alpha=1) 
Copy the code

<2> Get subgraph for drawing

import matplotlib.pyplot as plt

ax1 = ax[0.0]  Get the first subgraph from the ax collection list and name it ax1
ax2 = ax[0.1]  Get the first subgraph from the ax collection list and name it ax2
ax3 = ax[1.0]  Get the first subgraph from the ax collection list and name it ax3
ax4 = ax[1.1]  Get the first subgraph from the ax collection list and name it ax4
Copy the code

<3> Set the title for the subgraph

# add a title to the subimage, which can also have a number of parameters, such as size, font, color, transparency, etc
# Usually black, opacity is 1
ax1.set_title("Figure 1",fontsize=30,color='black',alpha=1)
Copy the code

<4> Draw on the subgraph

    # Of course you can draw line chart, also can draw other graphics
    ax1.plot([1.2.3],label="111") # the lable argument here is the text that explains the line
    ax1.plot([4.5.6],label="222") 
Copy the code

<5> Display legend

    # If you draw multiple lines in a graph, you need to display a legend to distinguish between them.
    # Draw legend depends on the label parameter when drawing above
    #title: Legends are also titled. This parameter may or may not be written
    # fancyBox: Determines whether the box for the legend is rectangular or rounded, if fancybox=True, rounded, otherwise rectangular
    The loC parameter determines the position of the legend,
    The #loc parameter has options like 'lower left','lower right','best','left','right','upper left','upper Right ', etc.
    The #prop argument is a dictionary object that contains parameters such as font, thickness, and size
    ax1.legend(title='xxx',
               fancybox=False,
               loc='lower left',
               prop={'family':'SimHei'.'weight':'normal'.'size':15}) 
Copy the code
All position options for the legend are as follows:Copy the code

<6> Remove legend

    ax1.legend_.remove()  Remove the legend from subgraph AX1
Copy the code

<7> Sets the name label of the XY axis

    The rotation is 0 by default, and the rotation is horizontal
    ax1.set_xlabel("data_month",fontsize=20,color='red',rotation=0)  # Rotation is 0 by default
    ax1.set_ylabel("Number of active Staff",fontsize=20,color='red',rotation=90) Rotation is 90 by default
Copy the code

< > 8 grid lines

    If necessary, grid lines can be set so that the display is more perfect
    Ax1. grid(which='major',axis='x',color='gray',linestyle='-.', lineWidth =1)
    The # which parameter indicates whether to draw grid lines for primary or secondary scales or for both scales
    The axis parameter determines which axis to draw grid lines on,
    # color determines the color of the gridline,
    # linestyle is the shape of a grid line,
    # lineWidth is the thickness
    There are a few parameters that need to be explained:
        # which contains 3 parameters ['major','minor','both']
        # The rear distribution is the secondary scale and the primary and secondary scales are displayed
        # axis also has 3 parameters ['x','y','both'], which are used to grid the x, y, x, and y axes respectively
    ax1.grid(which='major',axis='x',color='gray',linestyle='-',linewidth=1) 
    ax1.grid(which='major',axis='y',color='gray',linestyle='-',linewidth=1)  
    ax1.grid(which='major',axis='both',color='gray',linestyle='-',linewidth=1)  
Copy the code

<9> Add horizontal and vertical lines

    ax1.axvline(x=1,ls="-",c="red")  # Add vertical lines
    ax1.axhline(y=1,ls="-",c="red")  # Add horizontal lines
Copy the code

<10> Add explanatory text

    The first two parameters represent the location, and where to write the explanatory text
    Fontdict is a dictionary object with more detailed parameters such as color, size, font, shape, transparency, etc
    There are times when we don't want the explanatory text to stand out, but we have to write it, so the alpha parameter comes into play.
    font = {'size':20.'color':'red'.'family':'SimHei'.'style':'italic'."alpha":0.3.'ha':'left'.'va':'center'}
    The ha parameter determines the position in the horizontal direction, with left, center, and right3 options
    #va = 'top', 'bottom', 'center', 'baseline
    
    ax1.text(1.2."The red line is the date on which the focus day was implemented.",fontdict=font)
    
    # you can split the fields separately, either way.
    ax1.text(0.25.3."Marginal area",fontsize=25,color='gray',ha='center',va='center',alpha=0.3)
Copy the code

<11> Set the scale of the XY axis, and you can also set the precision

    # let x and y show all the points
    In some cases, only a few points are displayed on the xy axis, and other points are ignored
    # This situation can sometimes seem unclear
    # so, we can make all the points show first
    # Of course, the argument is a list, which can be a real X, or you can set it yourself
    ax1.set_xticks(range(20),0.5)  The X-axis displays all ranges (20). The second parameter is precision, which is optional. If not, the actual first parameter is displayed
    ax1.set_yticks(range(20),0.5)  The Y-axis displays all ranges (20). The second parameter is precision, which is optional.

    start, end = ax1.get_xlim()   First get the X-axis range of subgraph AX1, then reset the X-axis scale distribution
    ax1.xaxis.set_ticks(np.arange(start, end,1))  # the scale shown on the X-axis has a span of 1

    start, end = ax1.get_ylim()   First get the X-axis range of subgraph AX1, then reset the X-axis scale distribution
    ax1.yaxis.set_ticks(np.arange(start,end,1))  # the scale shown on the X-axis has a span of 1
Copy the code

<12> Sets the range of the XY axis

    ax1.set_xlim(0.50)  Set the range from 0 to 50
    ax1.set_ylim(0.30)
Copy the code

<13> Show the y-scale on the left or right

    ax1.yaxis.tick_left()  # display on the left, default is left, if the default is used, do not write this command
    ax1.yaxis.tick_right() # is displayed on the right
Copy the code

<14> Draw another subgraph, each of which is drawn the same way.

    ax2 = ax[0.1]
    ax2.plot([11.22.33],label="China")
    ax2.plot([1.2.3],label="Little Japan")
Copy the code

< 15 > storage

    plt.savefig('.. /data/1.png')
    plt.show()
Copy the code

<16> Remove the border

A axes subgraph is composed of 4 edges. We can artificially determine whether these 4 edges are displayed or not. There are two ways to delete the edgesCopy the code
A axes subgraph is provided by4It's made up of edges. We can artificially determine this4Whether the bar border is displayed< 1 > method # 1
    # gCA = get current axes and get the current painting area. If there is only one AXES in the FIG, this code can also be omitted
    ax = plt.gca()  
    #<1> Remove the border by set_color(" None ")
    ax1.spines["right"].set_color("none") # Remove the right edge
    ax1.spines["top"].set_color("none")   # Remove the top edge
    ax1.spines["left"].set_color("none")   # Remove the top edge
    ax1.spines["bottom"].set_color("none")   # Remove the top edge
    
    
    <2> method2
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
Copy the code

<17> Move the axes

    Sometimes to draw a quadrant map, it is necessary to move the coordinate axes
    # "data" means to move the left side to x=0
    ax1.spines["left"].set_position( ("data".0))# "data" moves the bottom edge to y=0.05
    ax1.spines["bottom"].set_position( ("data".0.05))Copy the code

<18> Clear the canvas

    plt.cla() # clear axes, clear axes, i.e. active axes in the current figure, but the other axes remain unchanged.
    plt.clf() # clear FIG, clears all axes of the current figure, but does not close this FIG, so it can continue to be applied to other plots.
    plt.close() # Turn off FIG, if not specified, to the current FIG.
Copy the code

4/plt.rcParams

PLT (matplotlib.pyplot) uses rc configuration files to customize various default properties of graphs, called RC configurations or RC parameters. The rc parameter allows you to modify default properties, including form size, points per inch, line width, color, style, axis, coordinate and network properties, text, font, and more. The rc parameter is stored in a dictionary variable and is accessed as a dictionary.Copy the code

5 / line chart

Application Scenario: There is a certain trend, whether it is a time trend or a big trend. The X-axis represents the trend, and each X-axis scale has only one data pointCopy the code
import matplotlib.pyplot as plt
import pandas as pd

data_df = pd.read_excel(".. /data/final.xlsx")
print(data_df.head())

Initialize the canvas and subgraph
fig,ax= plt.subplots(2.1,figsize=(12.6),dpi=150)

Add the main title
plt.subplots_adjust(hspace=0.5)

Figure 1 #
ax1 = ax[0]
x = data_df["Commuting distance"].tolist()
y1 = data_df["Average hours spent not taking a taxi on weekdays"].tolist()
y2 = data_df["Average taxi time on weekdays"].tolist()

ax1.plot(x,y1,label="Average work time on weekdays when you're not taking a taxi.")
ax1.plot(x,y2,label="Average taxi time on weekdays.")
ax1.set_title("Commuting Distance _ Working hours (20210101~20210707)",fontsize=15,color='black',alpha=1)

ax1.set_xlabel("Commuting distance",fontsize=12,color='black',rotation=0)  # Rotation is 0 by default
ax1.set_ylabel("Average hours",fontsize=12,color='black',rotation=90) Rotation is 90 by default

ax1.legend(loc='lower right',prop = {'family':'SimHei'.'weight':'normal'.'size':6})
ax1.grid(axis='y',color='gray',linestyle='-',linewidth=1) 
ax1.xaxis.set_ticks(x)  # Set the x scale

Figure 2 #
ax2 = ax[1]
x = data_df["Commuting distance"].tolist()
y = data_df["Number of employed persons"].tolist()
ax2.plot(x,y,label="Number of employed persons")
ax2.set_title("Commuting Distance _ Number of employed (as of 20210707)",fontsize=15,color='black',alpha=1)
ax2.set_xlabel("Commuting distance",fontsize=12,color='black',rotation=0)  # Rotation is 0 by default
ax2.set_ylabel("Number of employed persons",fontsize=12,color='black',rotation=90) Rotation is 90 by default

ax2.legend(loc='lower right',prop = {'family':'SimHei'.'weight':'normal'.'size':6})
ax2.grid(axis='y',color='gray',linestyle='-',linewidth=1)  
ax2.xaxis.set_ticks(x)  # Set the x scale

plt.savefig(".. /data/ housing allowance _ commuting distance _ working hours.pdf",dpi=150.format="pdf")   # Save FIG, dPI is the resolution, format is the storage form

plt.show()
Copy the code

6 / scatter plot

Application scenario: You can view data distribution and general trend. Each x scale can have multiple data points.Copy the code
import matplotlib.pyplot as plt 

fig,ax = plt.subplots(num="xxxx",figsize=(30.8),dpi=200) 

Set_ha = horizontal; va = up/down
ax.set_title(Scatter plot).set_ha("center")  

# Set the range of the X-axis and Y-axis, depending on the actual situation
ax.set_xlim(-3.12) 
ax.set_ylim(0.1.1) 

Get the minimum and maximum of the range of x and y
xmin, xmax = ax.get_xlim() 
ymin, ymax = ax.get_ylim()

The scale displayed on the X-axis and Y-axis, span 1
ax.xaxis.set_ticks( np.arange(xmin,xmax,1) ) 
ax.yaxis.set_ticks( np.arange(ymin,ymax,0.1))X-axis y axis name
ax.set_xlabel("Month") 
ax.set_ylabel("The number")  

# Set horizontal and vertical lines
ax.axhline(y=4,ls=":",c="yellow")  
ax.axvline(x=4,ls="-",c="green")   

# add text
ax.text(x,y,text,fontsize=25,color="gray",ha="center",va="center",alpha=0.3)

# draw function
ax.scatter(list.# x
           list.# y
           label="xxxx".# legend
           c = xxx,  # Scatter color
           edgecolor='xxx'.Scatter edge color, can not
           facecolor='xxx'.Scatter fill color, can not
           alpha = 1.# Scatter transparency
           s = 60 )  # Scatter size

# display legend
Ax.scatter () must have a label parameter to scatter the legend
ax.legend(title="Name",fancybox=False,loc='lower right')  
# Parameter description:
    #title is the title of the legend
    # fancyBox #fancybox #fancybox #fancybox #fancybox
    #loc is the location of the legend
Copy the code

7 / pie chart

import pandas as pd
import nunpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

Initialize the total canvas and subgraph
fig,ax = plt.subplots(figsize=(12.6),dpi=200)
fig.suptitle("General Heading _ Pie Chart",fontsize=50,color='black',alpha=1) 

# Add subgraph title
ax.set_title('Programming Language Index For August 2018',fontsize=50,colosr='black')

# data
data = [0.1688.0.1496.0.0747.0.0699.0.0476.0.0354.0.0292.0.0241.0.0231.0.0140.0.3632]

# label
labels = ['Java'.'C'.'C++'.'Python'.'Visual Basic.NET'.'C#'.'PHP'.'JavaScript'.'SQL'.'Assembly langugage'.'other']

# Separate out the language ranked fourth (Python)
explode =[0.0.0.0.3.0.0.0.0.0.0.0]

# Use custom colors
colors = ['red'.'pink'.'magenta'.'purple'.'orange']

# Standardize the horizontal and vertical axes to ensure that the pie chart is a perfect circle, otherwise it is an ellipse
ax.set_aspect('equal')

# control the X-axis and Y-axis range (used to control the center of the pie chart, radius)
After setting the center position and radius, finally hide the xy axis
ax.set_xlim(0.8)
ax.set_ylim(0.8)

# do not show borders
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['bottom'].set_color('none')

# Chart the pie
ax.pie(x=data, # draw data
       labels=labels, # add a tag
       explode=explode, # Highlight
       colors=colors,  # Fill color
       autopct='%.2f%%'.Set the percentage format to 2 decimal places
       pctdistance=0.8.Set the distance between the percentage tag and the center of the circle
       labeldistance=1.0.Set the distance between the label and the center of the circle
       startangle=180.# Set the initial pie Angle
       center=(4.4),  # Set the position of the center of the circle (equivalent to the range of X and Y axes)
       radius=3.8.# Radius size (equivalent to the range of X and Y axes)
       shadow=False.# if there is shadow, default is no shadow
       counterclock= False.# Indicates whether the value is counterclockwise. False indicates clockwise
       wedgeprops= {'linewidth':1.'edgecolor':'green'},# set the attribute values for the inner and outer boundaries of the pie chart
       textprops= {'fontsize':12.'color':'black'},# Set the attribute value of the text tag
       frame=1 # whether to display the circle of pie chart,1 indicates display)

# do not display the scale values of X and Y axes
ax.set_xticks(())
ax.set_yticks(())

# Display graphics
plt.show()
Copy the code

8 / histogram

Application scenario: Classified data. For example, in a certain Olympics, look at the number of MEDALS won by each country.Copy the code
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt 

    plt.rcParams["font.family"] = ["Arial Unicode MS"]  # specify the font
    plt.rcParams['font.sans-serif'] = ["Microsoft JhengHei"] YaHei JhengHei
    plt.rcParams['axes.unicode_minus'] = False     # normal display negative sign

    # fetch data
    data_df = pd.read_excel('/Users/houzhen/Desktop/fangjia.xlsx')

    Do the necessary processing on the data
    # sort
    data_df = data_df.sort_values(by="price",ascending=True)  # Order by price from largest --> smallest

    fig,ax = plt.subplots(figsize=(12.6),dpi=144,facecolor='black',edgecolor='black')
    ax.set_title(List of Housing Prices in Prefecture-level Cities in Shandong Province (Yuan/square meter)).set_ha("center")

    Note the difference between the bar() function and the barh() function
    The # bar() function is vertical, and the barh() function is horizontal
    Ax.barh () is assigned to a variable so that we can adjust the bar graph using this variable, and we can adjust any of the columns
    x = data_df['region'].tolist() # The X-axis is cities
    y = data_df['price'].tolist()  # on my Y-axis, I have house prices
    barh = ax.barh(x,y,color=np.random.rand(4) )

    barh[-1].set_color("r")  Set the color of the last column to red

    # Add data annotations to the bar chart
    for y, x in enumerate(data_df['price'].tolist()):
       ax.text(x+100, y-0.2."%s" % x)

    # Since the columns drawn here are horizontal, all borders can be removed
    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)

    ax.tick_params(labelsize=14)
    ax.set_xlabel("Total price")
    ax.set_ylabel("Area")

    plt.show()
Copy the code

9 / box figure

The main function of box graph is to find the overall distribution and dispersion of data, including upper and lower limits, each quantile, mean and outlier. As shown below:Copy the code

The basic grammarCopy the code
   plt.boxplot( x, # specify the data to draw the box diagram
                notch=None.# Whether the box plot is in the form of a notch
                sym=None, specify the shape of the exception vert=None, whether the boxplot should be placed vertically whis=None, specify the distance between the upper and lower quartilesNone, specify the position of the boxplot widths=None, specify the width of the boxplot patch_artist=None, whether to fill the box color; bootstrap=None, 
                usermedians=None, 
                conf_intervals=None, 
                meanline=None, 
                showmeans=NoneShowcaps =NoneShowbox = whether to display the top and bottom lines of the boxline diagramNone, whether to display the box diagram of the box showfliers=None, whether to display the exception value boxprops=None, set box properties, such as frame color, fill color, etc. labels=None, add the tag flierprops= for the boxplotNone, set the exception property medianprops=None, set the property of the median to meanprops=None, set the property capprops=None, set the properties of the top and end lines of the boxline diagram whiskerprops=None, sets the required attribute manage_xticks=True, 
                autorange=False, 
                zorder=None, 
                hold=None, 
                data=None)
Copy the code

Basic usage:import pandas as pd
    import matplotlib.pyplot as plt

    # fetch data
    data = pd.read_excel('D:\\pythondata\\learn\\matplotlib.xlsx')
    box_1, box_2, box_3, box_4 = data['income _Jay], data['income _JJ'], data['income _Jolin'], data['income _Hannah']
	
    fig,ax = plt.subplots(figsize=(12.6),api=144)
    fig.suptitle("General title",fontsize=50,color='black',alpha=1) 
    
    ax.set_title("xxx").set_ha("center")
    labels = 'Jay'.'JJ'.'Jolin'.'Hannah'# legend
    ax.boxplot([box_1, box_2, box_3, box_4],labels = labels)           
    
    plt.show()
Copy the code
   # pandas' dataframe draws box diagrams
   # syntax:
   DataFrame.boxplot(column=None, specifies the columns to be boxed. The default is all columns by=None, 
                      ax=None,
                      showmeans=False.# display average value
                      fontsize=None, 
                      rot=0, 
                      grid=True, 
                      figsize=None, 
                      layout=None, 
                      return_type=None)
Copy the code
The parameters are described as followsCopy the code

   Example # :
   import pandas as pd
   import matplotlib.pyplot as plt

   data_df = pd.read_excel('/Users/houzhen/Desktop/fangjia.xlsx')

   fig,ax = plt.subplots(figsize=(12.6),dpi=144)
   fig.suptitle("General title",fontsize=50,color='black',alpha=1) 
   ax.set_title("xxx").set_ha("center) data_df.boxplot(showmeans=True)Copy the code

10 / radar map

Radar maps are generally used to compare different people or things in multiple dimensions. The value range of each dimension of the radar map is generally [0,1], so the data generally need to be normalized or standardized. There are two ways to draw a radar map: <1> Draw a radar map and compare multiple people on a radar map. <2> Draw multiple radar maps and compare multiple people in multiple radar maps. For example, draw a radar map of the following dataCopy the code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Set some parameters via plt.rcparams
plt.rcParams["font.family"] = ["Arial Unicode MS"]
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False

# fetch data
basic_df = pd.read_excel("/Users/houzhen/Desktop/111xlsx.xlsx",encoding="gb18030")

# Data normalization
# Data normalization for each feature
feature_name = basic_df.columns.tolist()[1:]
for each_feature in feature_name:
    max_value = basic_df[each_feature].max(a)A maximum #
    min_value = basic_df[each_feature].min(a)# the minimum
    basic_df[each_feature] = basic_df[each_feature].apply(lambda v: (v - min_value) / (max_value - min_value))

What are the features of each Angle of the radar map
feature_name = basic_df.columns.tolist()[1:]  

# Number of features
dataLength = len( feature_name )

# data
data = basic_df.values.tolist()

# Angle
angles = np.linspace(0.2*np.pi,dataLength,endpoint=False) 

# Start drawing radar maps
Initialize a total canvas
fig = plt.figure(figsize=(13.7))  

# Add a subgraph to this canvas.
The # polar=True argument creates polar coordinates. The radar map needs to create polar coordinates
# Polar is False by default, that is, normal 2d cartesian coordinates are created
ax = fig.add_subplot(111,polar=True) 

Add title to subgraph, set subgraph, size, etc
# Of course, these parameters can also be set above.
ax.set_title("xxx",fontproperties="SimHei",fontsize=16)

for i in data: # Data for each person, and each row of data
    temp_data = np.concatenate( (i[1:], [i[1:] [0]]) )
    temp_angles = np.concatenate((angles,[angles[0]]))

    ax.set_thetagrids(temp_angles * 180/np.pi,feature_name,fontproperties="SimHei")
    ax.plot(temp_angles,temp_data,color=np.random.rand(4),linewidth=2,label=i[0]) 
    ax.set_rlim(0.1) Set the radius to 0,1.

ax.legend(title='xxx'.# Legend title
          fancybox=False.Is the border of the legend rectangular or rounded
          loc='lower left'.# Legend position
          prop={'family':'SimHei'.'weight':'normal'.'size':15})  # display legend
plt.show()
Copy the code

11/ Pareto diagram

Pareto is a popular diagarm in Excel and Tableu. Pareto diagrams can be drawn easily in Excel, but I found no easy way to draw them in Python. After the Pareto diagram is drawn, we can know both the value of any category and which category accounts for the first XX % of the total. For example, in a certain Olympic Games, we know how many gold MEDALS each country won, and we can also know which countries won the top 80% of gold MEDALS.Copy the code
   import pandas as pd 
   import matplotlib.pyplot as plt
   from matplotlib.ticker import PercentFormatter

   data_df = pd.DataFrame(data={'countries': ['USA'.'Canada'.'Russia'.'UK'.'Belgium'.'Mexico'.'Germany'.'Denmark'].'Gold medal count': [177.0.7.0.4.0.2.0.2.0.1.0.1.0.1.0]})

   # Sort gold MEDALS in descending order
   data_df = data_df.sort_values(by='Gold medal count',ascending=False)
   data_df["cumpercentage"] = data_df["Gold medal count"].cumsum() / df["Gold medal count"].sum(*)100  The # cumsum() function is cumulative

   fig,ax = plt.subplots(figsize=(10.6),dpi=100)
   ax.set_title(Pareto diagram,fontproperties="SimHei",fontsize=16)
   x = data_df['countries']
   y = data_df["Gold medal count"]
   ax.bar(x,y,color="C0")

   The #twinx() function represents the shared X-axis
   #twiny() denotes sharing the Y-axis
   # shared means that the X-axis uses the same scale line

   ax2 = ax.twinx()  Share the x axis with ax
   y = data_df["cumpercentage"]
   ax2.plot(x,y,color="C1",marker="D",ms=7)
   ax2.yaxis.set_major_formatter( PercentFormatter() ) # change floating point to percentage

   # Finally, make some adjustments to the color
   ax.tick_params(axis="y",colors="C0")  The # tick_params() function adjusts parameters on the scale
   ax2.tick_params(axis="y",colors="C1")

   plt.show()
Copy the code