Draw multiple charts on one canvas

Matplotlib When the module draws the chart , By default, a canvas is created first , Then display the chart drawn in the canvas .

If you want to draw multiple charts in one canvas , have access to subplot() Function divides the canvas into several areas , Then draw different charts in each area .

subplot() The argument to the function is 3 An integer number :

  • The first 1 A number means dividing the whole canvas into several lines ;
  • The first 2 A number means that the whole canvas is divided into several columns ;
  • The first 3 A number represents the number of areas in which to plot the chart , The numbering of areas is from left to right 、 The order from top to bottom , from 1 Numbered starting .

The demo code is as follows :

import matplotlib.pyplot as plt
# If there are Chinese characters in the value , You must add these two lines of code before you draw the chart 
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1 month ', '2 month ', '3 month ', '4 month ', '5 month ', '6 month ', '7 month ', '8 month ', '9 month ', '10 month ', '11 month ', '12 month ']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.subplot(2, 2, 1)
plt.pie(y, labels = x, labeldistance = 1.1, startangle = 90, counterclock = False)
plt.subplot(2, 2, 2) # Parameters width Used to set the width of the column , The default value is 0.8. If set to 1, The columns will be closely connected ; If set to greater than 1 Number of numbers , The columns will overlap each other
plt.bar(x, y, width = 0.5, color = 'r')
plt.subplot(2, 2, 3) # Parameters color Used to set the filling color of the column , See the following description for specific values
plt.stackplot(x, y, color = 'r')
plt.subplot(2, 2, 4)
plt.plot(x, y, color = 'r', linestyle = 'solid', linewidth = 2, marker = 'o', markersize = 10)
plt.show()

Output results :

It is defined by the English word of color name or its abbreviation 8 Two basic colors , Specific view :

Add chart elements

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1 month ', '2 month ', '3 month ', '4 month ', '5 month ', '6 month ', '7 month ', '8 month ', '9 month ', '10 month ', '11 month ', '12 month ']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90] # It's labeled here
plt.bar(x, y, width=0.6, color='r', label=' sales ( Ten thousand yuan )') # The title is added here ,loc It can also be right and left
plt.title(label=' Sales comparison chart ', fontdict={'family': 'KaiTi', 'color': 'k', 'size': 30}, loc='center') # Labels on coordinates
plt.xlabel(' month ', fontdict={'family': 'SimSun', 'color': 'k', 'size': 20}, labelpad=20)
plt.ylabel(' sales ', fontdict={'family': 'SimSun', 'color': 'k', 'size': 20}, labelpad=20) # legend() Function to add a legend
plt.legend(loc='upper left', fontsize=15) # zip() Function to take iteratable objects as parameters , Package the corresponding elements in the object into tuples , Then return a list of these tuples .
for a,b in zip(x, y):
# text() The function adds text at the specified position in the chart coordinate system . Parameters ha yes horizontalalignment For short , There should be va
plt.text(x=a, y=b, s=b, ha='center', va='bottom', fontdict={'family': 'KaiTi', 'color': 'k', 'size': 20})
plt.show()

Output results

Bubble chart

A bubble chart is a chart showing the relationship between three variables , In fact, it is upgraded and transformed on the basis of scatter diagram , In the original x Coordinates and y Coordinates are based on two variables , Introduce a third variable , And it's represented by the size of the bubble .

pip install openpyxl

Product sales statistics .xls Content

The product name Sales volume ( Pieces of ) sales ( element ) Gross profit margin (%)
A pair of jeans 125 6800 30
dress 278 5600 20
Motion pants 366 7800 35
shorts 452 5800 10
Short skirt 365 5400 50
overalls 258 10000 22
skirt 369 3600 15
Wide-legged pants 566 7800 8

The code is as follows :

import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel(' Product sales statistics .xls')
n = data[' The product name ']
x = data[' Sales volume ( Pieces of )']
y = data[' sales ( element )']
z = data[' Gross profit margin (%)']
plt.scatter(x, y, s=z * 300, color='r', marker='o')
plt.xlabel(' Sales volume ( Pieces of )', fontdict={'family': 'Microsoft YaHei', 'color': 'k', 'size': 20}, labelpad=20)
plt.ylabel(' sales ( element )', fontdict={'family': 'Microsoft YaHei', 'color': 'k', 'size': 20}, labelpad=20)
plt.title(' Sales volume 、 Sales vs. gross margin ', fontdict={'family': 'Microsoft YaHei', 'color': 'k', 'size': 30}, loc='center')
for a, b, c in zip(x, y, n):
plt.text(x=a, y=b, s=c, ha='center', va='center', fontsize=15, color='w')
plt.xlim(50, 600)
plt.ylim(2900, 11000)
plt.show()

Output results :

Combination chart

A combination chart is to draw multiple charts in a coordinate system , Its implementation is also very simple , In the use of Matplotlib The function in the module sets multiple groups when drawing a chart y The coordinate value is enough .

Sales performance table .xls

month sales ( Ten thousand yuan ) Year on year growth rate
1 month ¥36.00 10%
2 month ¥25.00 8%
3 month ¥36.12 20%
4 month ¥69.30 50%
5 month ¥26.90 15%
6 month ¥32.00 11%
7 month ¥45.00 26%
8 month ¥56.00 13%
9 month ¥25.60 4%
10 month ¥36.21 5%
11 month ¥25.00 7%
12 month ¥59.00 30%
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel(' Sales performance table .xlsx')
x = data[' month ']
y1 = data[' sales ( Ten thousand yuan )']
y2 = data[' Year on year growth rate ']
plt.bar(x, y1, color = 'c', label = ' sales ( Ten thousand yuan )')
plt.legend(loc = 'upper left', fontsize = 15) # Use twinx() Function to add secondary axes to the chart
plt.twinx()
plt.plot(x, y2, color = 'r', linewidth = 3, label = ' Year on year growth rate ')
plt.legend(loc = 'upper right', fontsize = 15)
plt.show()

Output results :

Histogram

Histograms are used to show the distribution of data , Use Matplotlib Module hist() Function to draw histograms

Customer age statistics .zip

import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel(' Customer age statistics .xlsx')
x = data[' Age ']
plt.hist(x, bins = 9)
plt.xlim(15, 60)
plt.ylim(0, 40)
plt.title(' Age distribution histogram ', fontsize = 20)
plt.xlabel(' Age ')
plt.ylabel(' The number of ')
plt.grid(b = True, linestyle = 'dotted', linewidth = 1)
plt.show()

Output results

Radar map

Radar charts can compare and analyze multiple indicators at the same time . The chart can be seen as one or more closed broken lines , therefore , Use the plot() Function can also draw radar map .

Statistical table of automobile performance index score .xlsx

Performance evaluation index A brand B brand C brand
dynamic 1 3 10
Fuel economy 2 6 7
Braking 1 10 5
Handling stability 3 10 2
Ride comfort 2 6 1
Passability 4 7 2
Security 8 2 1
Environmental protection 9 1 3
Convenience 10 3 0
comfort 8 2 1
economy 4 1 10
Capacity 2 2 8

The code is as follows :

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel(' Statistical table of automobile performance index score .xlsx') data = data.dropna(axis=1)
data = data.set_index(' Performance evaluation index ')
data = data.T
data.index.name = ' brand ' def plot_radar(data, feature):
columns = [' dynamic ', ' Fuel economy ', ' Braking ', ' Handling stability ', ' Ride comfort ', ' Passability ', ' Security ', ' Environmental protection ', ' Convenience ', ' comfort ', ' economy ', ' Capacity ']
colors = ['r', 'g', 'y'] # Set the angle of the radar chart , Used to bisect a plane
# linspace(1,10,x) establish 1-10 The equal difference array of , The number is x, Default 50 individual ;endpoint Parameter specifies whether to include a final value , The default value is True, Contains the final value .
angles = np.linspace(0.1 * np.pi, 2.1 * np.pi, len(columns), endpoint = False) # Close the radar map
angles = np.concatenate((angles, [angles[0]])) # figsize: Appoint figure Width and height , In inches ;
figure = plt.figure(figsize = (6, 6)) # Set to polar format ;subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw) Create a single subgraph , The following two sentences have the same effect
ax = figure.add_subplot(111, polar=True)
# ax = figure.add_subplot(1, 1, 1, projection = 'polar') for i, c in enumerate(feature):
stats = data.loc[c] stats = np.concatenate((stats, [stats[0]])) ax.plot(angles, stats, '-', linewidth = 2, c = colors[i], label = str(c))
ax.fill(angles, stats, color = colors[i], alpha = 0.75) # bbox_to_anchor This parameter , You can put the legend on the outside of the diagram
# bbox_to_anchor: Express legend The location of , The former means left and right , The latter means up and down .
# When using this parameter .loc Will no longer function normally ,ncol=3 The legend is shown in three columns .
ax.legend(loc = 4, bbox_to_anchor = (1.15, -0.07)) # Set the polar range
ax.set_ylim(0,10)
# ax.set_yticklabels([2, 4, 6, 8, 10]) # Add labels for each trait
columns = np.concatenate((columns, [columns[0]]))
ax.set_thetagrids(angles*180/np.pi, columns, fontsize = 12) # Add the title
plt.title(' Car performance index radar chart ') plt.show()
return figure
figure = plot_radar(data, ['A brand ', 'B brand ', 'C brand '])
# figure = plot_radar(data, ['B brand '])

Tree view

The area of the tree through the rectangle 、 Arrangement and color intuitively display the data proportion relationship of multiple items . To draw the chart , Need to be used in combination Matplotlib Module and squarify modular .

import squarify as sf
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = [' Shanghai ', ' Beijing ', ' Chongqing ', ' Chengdu ', ' nanjing ', ' Qingdao ', ' Changsha ', ' wuhan ', ' Shenzhen ']
y = [260, 45, 69, 800, 290, 360, 450, 120, 50]
colors = ['lightgreen', 'pink', 'yellow', 'skyblue', 'cyan', 'silver', 'lightcoral', 'orange', 'violet']
percent = ['11%', '2%', '3%', '33%', '12%', '15%', '18%', '5%', '2%']
chart = sf.plot(sizes = y, label = x, color = colors, value = percent, edgecolor = 'white', linewidth = 2)
plt.title(label = ' Distribution and proportion of city sales ',fontdict = {'family' : 'KaiTi', 'color' : 'k', 'size' : 25})
plt.axis('off')
plt.show()

Box chart

A box chart is a statistical chart used to show the distribution of data , Named for its shape like a box . Use Matplotlib Module boxplot() Function to draw a box graph .

data

date Chengdu Shanghai Beijing Chongqing nanjing
1 month 1 Japan 25 50 52 25 50
1 month 2 Japan 12 58 56 26 56
1 month 3 Japan 26 60 100 78 58
1 month 4 Japan 23 78 125 45 87
1 month 5 Japan 18 36 108 46 50
1 month 6 Japan 15 69 100 50 60
1 month 7 Japan 19 41 85 53 26
1 month 8 Japan 20 52 85 61 36
1 month 9 Japan 26 53 87 87 69
1 month 10 Japan 27 69 86 25 78
1 month 11 Japan 28 78 45 16 75
1 month 12 Japan 54 80 78 69 80
1 month 13 Japan 50 52 73 68 81
1 month 14 Japan 51 26 62 45 45
1 month 15 Japan 52 28 65 40 65
1 month 16 Japan 36 57 90 50 63
1 month 17 Japan 38 56 96 60 69
1 month 18 Japan 45 89 94 36 64
1 month 19 Japan 40 84 25 52 65
1 month 20 Japan 41 85 36 54 45
1 month 21 Japan 26 80 68 58 52
1 month 22 Japan 29 75 78 56 59
1 month 23 Japan 36 50 70 52 80
1 month 24 Japan 33 25 52 57 29
1 month 25 Japan 31 36 51 69 36
1 month 26 Japan 15 64 58 54 29
1 month 27 Japan 18 56 68 25 90
1 month 28 Japan 25 54 78 36 78
1 month 29 Japan 14 50 90 78 71
1 month 30 Japan 39 44 95 56 75
1 month 31 Japan 48 49 84 25 76
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('1 Monthly sales statistics .xlsx')
x1 = data[' Chengdu ']
x2 = data[' Shanghai ']
x3 = data[' Beijing ']
x4 = data[' Chongqing ']
x5 = data[' nanjing ']
x = [x1, x2, x3, x4, x5]
labels = [' Chengdu ', ' Shanghai ', ' Beijing ', ' Chongqing ', ' nanjing '] # Parameters vert Used to set the orientation of the box graph ,True It means vertical display ,False Show horizontally ; Parameters showmeans Used to set whether the mean value is displayed ,True Shows the mean value ,False The mean value is not shown .
plt.boxplot(x, vert = True, widths = 0.5, labels = labels, showmeans = True )
plt.title(' Regions 1 Box chart of monthly sales ', fontsize = 20)
plt.ylabel(' sales ( Ten thousand yuan )')
plt.show()

In the box 5 Two horizontal lines and 1 The meaning of the dots is as follows :

  • Lower limit : The minimum of all data ;
  • Lower quartile : also called “ First quartile ”, It means to arrange all data from small to large 25% Value ;
  • Median : also called “ second quartile ”, It means to arrange all data from small to large 50% Value ;
  • Upper quartile : also called “ third quartile ”, It means to arrange all data from small to large 75% Value ;
  • ceiling : The maximum of all data ;
  • spot : The average of all data .

Rose chart

Rose chart can reflect data of multiple dimensions , It turns a column chart into a pie chart , With the same central angle , Show the index size by sector length . To draw a rose , It's also used to draw a column diagram bar() function .

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False # Set the wind speed distribution to 4 Intervals
index = ['0~0.5', '0.6~2.0', '2.1~4.0', '4.1~6.0'] # Set up 16 A direction
columns = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'] # seed() Function is used to generate the same random number
np.random.seed(0) # Create a 4 That's ok 16 Column DataFrame, The data is 30~300 Random numbers in the range , The row label is 6 The wind speed distribution range set by the line code , The column label is number 7 The direction of line code setting .
data = pd.DataFrame(np.random.randint(30, 300, (4, 16)), index = index, columns = columns)
N = 16 # Generate 16 The angle value in two directions
theta = np.linspace(0, 2 * np.pi, N, endpoint = False) # Used to calculate the width of the sector
width = np.pi / N
labels = list(data.columns)
plt.figure(figsize = (6, 6))
ax = plt.subplot(1, 1, 1, projection = 'polar')
for i in data.index:
radius = data.loc[i]
# Use bar() Function to draw 16 Root column , That's the fan , Parameters bottom Used to set the position of the bottom of each column , I'm going to set it to 0.0, It means to draw from the center of the circle .
ax.bar(theta, radius, width = width, bottom = 0.0, label = i, tick_label = labels) # Set up 0° The direction is “N”, The north
ax.set_theta_zero_location('N') # Set up a counter clockwise arrangement of the columns
ax.set_theta_direction(-1)
plt.title(' Rose chart of wind speed frequency in all directions ', fontsize = 20)
plt.legend(loc = 4, bbox_to_anchor = (1.3, 0.2))
plt.show()

[Python] Matplotlib Chart drawing and beautification techniques for more related articles

  1. python matplotlib The chart is partially enlarged

    import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes ...

  2. Python Matplotlib A solution to the problem of displaying Chinese characters in charts as frames

    http://blog.sina.com.cn/s/blog_662dcb820102vu3d.html http://blog.csdn.net/fyuanfena/article/details/ ...

  3. 【 turn 】 Use Python matplotlib Charting the stock market

    Reproduced source One . Preface matplotlib[1] It's famous python Drawing library , It provides a complete set of drawings API, Perfect for interactive drawing . I work in the process of dealing with stock data, such as drawing K Line, etc , So it will matplotlib The emissary of ...

  4. python+matplotlib Draw contours

    python+matplotlib Draw contours There are seven steps : There is one m*n A matrix of dimensions (data), The value of its element represents the height Construct two vectors :x(1*n) and y(1*m). These two vectors are used to construct the grid coordinate matrix ( Grid coordinate matrix ...

  5. python matplotlib.pyplot Draw an image

    imshow() It's drawing images imshow() The function format is : matplotlib.pyplot.imshow(X, cmap=None) X: The image or array to draw . cmap: Color map (colo ...

  6. python matplotlib plot The Chinese in the data cannot be displayed normally

    Forward from :http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...

  7. python matplotlib Basis of drawing

    Using Python When doing data analysis , It's essential to explore the application of data and graphs on the presentation of results . stay Python In general, we use matplotlib Module for chart making . Take care of it first ,matplotlib The structural principle of : mat ...

  8. Python - matplotlib Data visualization

    In many practical problems , It is often necessary to visualize the given data , Convenient for observation . Today, it's specifically for Python Data visualization module in --matplotlib The systematic arrangement of this content , Easy to find and use . This article comes from the analysis of < utilize python Into the ...

  9. matplotlib And scatter Draw a scatter

    # Use matplotlib.pyplot.scatter Draw a scatter import matplotlib.pyplot as plt from pylab import mpl # Set default font , It's being solved ...

  10. python Reptiles : Some common crawler techniques

    python Reptiles : Some common crawler techniques 1. Basic Web Capture get Method : post Method : 2. Using agents IP In the process of developing crawlers, we often encounter IP The situation of being sealed off , At this point, you need to use an agent IP; stay urllib2 There is... In the bag Pr ...

Random recommendation

  1. Spring3.0 Catalog

    (1)Spring Introduction (2)IoC/DI The evolution of basic ideas (3) In depth understanding of IoC/DI (4)Spring Simple demo

  2. KEIL And ADS1.2 coexistence

    What happened : The computer has been installed ADS1.2. Now install keil5 Compile one 32 Bit new Tang MCU program , The following error occurred : Error: L6411E: No compatible library exists ...

  3. 0-1 Knapsack problem and N The entanglement of the queen problem

    Yesterday, my classmates asked me to help him see an algorithm , as follows : Is it at first glance “0-1 knapsack ” Question , I think so , So I began to use this idea to think about how to calculate . But the algorithm is almost forgotten , Go back and make up for it , Also take this opportunity to review the algorithm , ...

  4. myeclipse 8.5 Crack

    step : (1) Create a new one java project project (2) stay src Create a new directory named MyEclipseGen Of Java file (MyEclipseGen.java) (3)MyEclipseGen.jav ...

  5. The finger of the sword Offer: Print from 1 To the biggest n digit

    subject : Enter the value n, Print in order from 1 To the biggest n digit , For example, the input n=3, From 1,2,3, Print all the way to 999 trap : If you use loop traversal 1- 999...9 And output in turn , The number of digits n When it is too large , Whether you deposit it in int or long or ...

  6. iOS Network and multithreading --8. The use of Baidu map ( Call the system browser )

    By calling the browser of the device , Open Baidu map // 1. Define a method , The ability to open Google Maps -(IBAction)openMaps { // 2. Define a string , As the current location of Baidu map abandoned NSSt ...

  7. hdu 5313 Bipartite Graph(dfs dyeing perhaps Union checking set )

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  8. vue.js window.removeEventListener remove

    vue Record the pit in the project , Want to remove window Of addEventListener, You need to put the back function Hang on to this On ,removeEventListener and addEventListen ...

  9. Android Custom control instance , Round head ( Gallery + tailoring + Set up ), The uploaded avatar is shown as a circle , Source code attached

    Android Project development often meet the need to achieve rounded or round picture function , If you only use the system's own ImageView Control obviously can't do this , So in the form of a series of articles, from simple to complex, we will introduce the realization of this function in an all-round way , Consolidate yourself ...

  10. &lt; Reprint &gt;Vim The register of ( Copy and paste with )

    https://blog.csdn.net/hk2291976/article/details/42196559 Remove highlights :noh