Drawing and beautifying skills of [Python] Matplotlib chart

Doraemon paradise 2021-02-20 19:54:00
drawing beautifying skills python matplotlib


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()

版权声明
本文为[Doraemon paradise]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210220195335004Z.html

  1. Python pandas: using pandas to add row data, that is, adding dictionary data to dataframe according to row data cycle
  2. Python:将Flask测试应用部署到Deta
  3. Python:OAuth2第三方登录之Github
  4. Python:使用pydantic库进行数据校验
  5. Python:免费IP归属地查询接口
  6. Python:使用user-agents库解析浏览器信息
  7. Python:cached_property缓存对象的属性
  8. Python:打包配置文件 setup.py 详解
  9. Python:ORM(Object Relational Mapper)模块汇总整理
  10. Python:datetime时间UTC时间转东八区
  11. Python: deploying flash test application to deta
  12. Python: oauth2 GitHub for third party login
  13. Python: using pydantic library for data verification
  14. Python: free IP home location query interface
  15. Python: parsing browser information using user agents Library
  16. Python:cached_ Property to cache the properties of an object
  17. Python: packaging configuration files setup.py Detailed explanation
  18. Python: ORM (object relational mapper) module summary
  19. Python: datetime, UTC, turn to District 8, East
  20. python tkinter 将某一目录下的所有图片插入到docx文件中
  21. Python Tkinter inserts all the pictures in a directory into the docx file
  22. 解决忽略VScode中Python插件pylint报错的问题
  23. To solve the problem of ignoring the error of Python plug-in in vscode
  24. python 毫秒级时间,时间戳转换
  25. Python millisecond time, timestamp conversion
  26. python try except 出现异常时,except 中如何返回异常的信息字符串
  27. When an exception occurs in Python try except, how to return the exception information string in except
  28. 手机最强Python编程神器,在手机上运行Python
  29. The strongest Python Programming artifact on mobile phones, running Python on mobile phones
  30. 2021年Python程序员薪资待遇如何?
  31. 「python安装」Windows上安装和创建python开发环境
  32. What is the salary of Python programmers in 2021?
  33. "Python installation" to install and create a python development environment on Windows
  34. python解决组合问题
  35. Python to solve the problem of composition
  36. Python中的Lasso回归之最小角算法LARS
  37. Lars, the least angle algorithm of lasso regression in Python
  38. 利用python提取网站曲线图数据
  39. Using Python to extract website graph data
  40. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
  41. Detailed usage of urllib in Python 3 (header, proxy, timeout, authentication, exception handling)
  42. python 第三方库paramiko
  43. python 第三方库paramiko
  44. Python third party library paramiko
  45. Python third party library paramiko
  46. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  47. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  48. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  49. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化
  50. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  51. Django learning - 27. Admin management background: optimize the data display of the list display page
  52. python day2
  53. python day2
  54. Python 内存泄漏问题排查
  55. Troubleshooting of Python memory leak
  56. Python 与 excel的简单应用
  57. Simple application of Python and excel
  58. Python 与 excel的简单应用
  59. Simple application of Python and excel
  60. 2.7万 Star!最全面的 Python 设计模式集合