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入门金融分析)
  2. Python:PyCharm选择性忽略PEP8警告
  3. Python: pychar selectively ignores pep8 warnings
  4. Django-模板
  5. Django template
  6. Python正则表达式大全
  7. 最全Python正则表达式来袭
  8. A python knowledge for Xiaobai
  9. 2. Flexible pandas index
  10. 1. Get to know pandas
  11. See how I use Python to create a magic with baby (one play can play for a day)?
  12. Wow, python can do real-time translation
  13. Python经典编程习题100例
  14. 100 examples of Python classic programming exercises
  15. Invincible, with Python for English teachers to develop a magic tool for English composition correction (support primary school to IELTS)
  16. 抖音数据采集教程,最全python库selenium自动化使用
  17. Pandas 11-综合练习
  18. Pandas 11 - comprehensive exercises
  19. Pandas基础|用户游览日志时间合并排序
  20. python自学 第三章 python语言基础之保留字、标识符与内置函数
  21. python学习例程3-函数
  22. Python GUI 之Tkinter小结 - 知乎
  23. Pandas foundation | user travel log time merge sort
  24. Chapter 3 reserved words, identifiers and built-in functions of the foundation of Python
  25. Tkinter summary of Python GUI - Zhihu
  26. 【Python常用包】itertools
  27. Itertools
  28. [Python] Matplotlib 图表的绘制和美化技巧
  29. Drawing and beautifying skills of [Python] Matplotlib chart
  30. Python序列之列表(一)
  31. Python解析库lxml与xpath用法总结
  32. Python解析库lxml与xpath用法总结
  33. Usage Summary of Python parsing library lxml and XPath
  34. Usage Summary of Python parsing library lxml and XPath
  35. Python web/HTML GUI
  36. Why is sanic better than Django flame?
  37. Wechat applet Python sends subscription message
  38. Invincible, with Python for English teachers to develop an English composition correction artifact (support primary school to IELTS)
  39. How can I use Python to create a magic with children (one can play for one day)?
  40. Pandas module
  41. Machine learning in Python - Boston house price forecast
  42. 50 Great Python modules
  43. Share the survival status of Python practitioners and tell you the real salary of general programmers
  44. Pandas basic operation update
  45. Python Programming day02 Python operator
  46. Usage Summary of Python parsing library lxml and XPath
  47. 1. First meeting pandas
  48. Conversion between Python and base conversion between Python and base
  49. Basics of Python
  50. Fundamentals of python (XIV): errors and exceptions
  51. Fundamentals of python (8): time related modules
  52. Fundamentals of python (I): necessary knowledge for getting started
  53. Operators in Python 3
  54. The list of national computer non graduate schools (captured by Python), just look at this one!
  55. Python data visualization: Seaborn
  56. Quick start pandas (lower)
  57. Operators in Python 3
  58. Python tarfile module
  59. Python basic syntax