Draw advanced charts with matplotlib

In addition to drawing simple charts, matplotlib can also draw some common advanced charts, including contour maps, vector field streamline maps, cotton swab maps, dumbbell maps, Gantt maps, population pyramid maps, funnel maps, and Sankey maps , treemap, and waffle chart. The following will introduce the relevant knowledge of drawing advanced charts in matplotlib in detail.
1. Draw a contour line:
a contour map is a closed curve formed by adjacent points with equal elevations on a topographic map. On a horizontal plane, it is scaled down to a drawing according to a certain ratio, which is often seen in valleys, mountain peaks or gradient descent algorithms.
In matplotlib, pyplot can use the contour) and contourf) functions to draw and fill contour maps, respectively.

import numpy as np
import matplotlib.pyplot as plt
def calcu_elevation(x1, y1):
    h = (1-x1/2  + x1 ** 5 + y1 ** 3)  * np.exp(-x1** 2 - y1** 2)
    return h
n = 256
x = np.linspace(-2, 2, n)
y = np.linspace(-2, 2, n)
x_grid, y_grid = np.meshgrid(x, y)
fig = plt.figure()
ax = fig.add_subplot(111)
con = ax.contour(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, colors='black')
ax.contourf(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, alpha=0.75, cmap=plt.cm.copper)
ax.clabel(con, inline=True, fmt='%1.1f', fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
plt.title('41')
plt.show()

  


2. Draw a vector field streamline diagram:
In matplotlib, pyplot can use the streamplot() function to draw a vector field streamline diagram.

import numpy as np
import matplotlib.pyplot as plt
y, x = np.mgrid[0:5:50j, 0:5:50j]
u = x
v = y
fig = plt.figure()
ax = fig.add_subplot(111)
ax.streamplot(x, y, u, v)
plt.title('41')
plt.show()

  


3. Draw a cotton swab map:
The cotton swab map is also called a matchstick map, a pin map or a lollipop map. It is formed by connecting a line segment (stem) and a marker symbol (stem head, which is a dot by default). Among them, the line segment represents the distance from the data point to the baseline, and the marker symbol represents the value of the data point. A swab chart is a variant of a column or bar chart and is primarily used to compare the relative positions of marker symbols rather than the length of line segments.
In matplotlib, pyplot can use the stem() function to draw cotton swab graphs.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(1, 16)
y = np.array([5.9, 6.2, 6.7, 7.0, 7.0, 7.1, 7.2, 7.4, 
              7.5, 7.6, 7.7, 7.7, 7.7, 7.8, 7.9])
labels = np.array(['宝骏310', '宝马i3', '致享', '焕驰', '力帆530', 
                   '派力奥', '悦翔V3', '乐风RV', '奥迪A1', '威驰FS', 
                   '夏利N7', '启辰R30', '和悦A13RS', '致炫', '赛欧'])
fig = plt.figure(figsize=(10, 6), dpi= 80)
ax = fig.add_subplot(111)
markerline, stemlines, baseline = ax.stem(x, y, linefmt='--', 
                                          markerfmt='o', label='TestStem', use_line_collection=True)
plt.setp(stemlines, lw=1)
ax.set_title('不同品牌轿车的燃料消耗量 41', fontdict={'size':18})
ax.set_ylabel('燃料消耗量(L/km)')
ax.set_xticks(x)
ax.set_xticklabels(labels, rotation=60)
ax.set_ylim([0, 10])
for temp_x, temp_y in zip(x, y):
    ax.text(temp_x, temp_y + 0.5, s='{}'.format(temp_y), ha='center', va='bottom', fontsize=14)    
plt.show()

  


4. Draw a dumbbell chart:
The dumbbell chart is also known as the DNA chart (the chart looks like a dumbbell horizontally, and looks like DNA vertically), which is mainly used to show the changes between two data points. The dumbbell graph can be regarded as a combination of a scatter graph and a line graph, and is suitable for comparing the "before" and "behind" positions of various items and the ranking of items.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_excel(r"C:\Users\Administrator\Desktop\health.xlsx")
df.sort_values('pct_2014', inplace=True)
df.reset_index(inplace=True)
df = df.sort_values(by="index")
def newline(p1, p2, color='black'):
    ax = plt.gca()    
    l = mlines.Line2D([p1[0], p2[0]], [p1[1],p2[1]], color='skyblue')
    ax.add_line(l)
    return l
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.scatter(y=df['index'], x=df['pct_2013'], s=50, color='#0e668b', alpha=0.7)
ax.scatter(y=df['index'], x=df['pct_2014'], s=50, color='#a3c4dc', alpha=0.7)
for i, p1, p2 in zip(df['index'], df['pct_2013'], df['pct_2014' ]):
    newline([p1, i], [p2, i])
ax.set_title("2013年与2014年美国部分城市人口PCT指标的变化率 41", fontdict={'size':12})
ax.set_xlim(0, .25)
ax.set_xticks([.05, .1, .15, .20])
ax.set_xticklabels(['5%', '10%', '15%', '20%'])
ax.set_xlabel('变化率')
ax.set_yticks(df['index'])
ax.set_yticklabels(df['city'])
ax.grid(alpha=0.5, axis='x')
plt.show()

  


5. Draw a Gantt chart:
Gantt charts are also known as bar charts and bar charts, which represent the sequence and duration of specific projects through activity lists and time scales. Gantt charts generally use time as the horizontal axis and projects as the vertical axis, which can visually display the progress of each project, so that managers can understand the remaining tasks of the project and evaluate the work progress.

import numpy as np
import matplotlib.pyplot as plt
ticks = np.array(['报告提交', '数据分析', '数据录入', '实地执行', 
                  '问卷确定', '试访', '问卷设计', '项目确定'])
y_data = np.arange(1, 9)
x_data = np.array([0.5, 1.5, 1, 3, 0.5, 1, 1, 2])
fig,ax = plt.subplots(1, 1)
ax.barh(y_data, x_data, tick_label=ticks, left=[7.5, 6, 5.5, 3, 3, 2, 1.5, 0], color='#CD5C5C') 
[ax.spines[i].set_visible(False) for i in ['top', 'right']]
ax.set_title("任务甘特图 41")
ax.set_xlabel("日期")
ax.grid(alpha=0.5, axis='x')
plt.show()

  


6. Draw a population pyramid:
The mouth pyramid map refers to a graph that describes the age and gender distribution of the population with an image similar to the ancient Egyptian pyramids, and is used to express the current status of the population and its development type. Population pyramid charts generally take age as the vertical axis and population as the horizontal axis, draw parallel horizontal rectangular bars on the left and right sides of the vertical axis from bottom to top according to the natural order of age, the left side of the vertical axis is male, and the right side is female.

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
df = pd.read_excel(r'C:\Users\Administrator\Desktop\population.xlsx')
df_male = df.groupby(by='Gender').get_group('Male')
list_male = df_male['Number'].values.tolist()      #  将ndarray 转换为 list
df_female = df.groupby(by='Gender').get_group('Female')
list_female = df_female['Number'].values.tolist()  # 将ndarray 转换为 list
df_age = df.groupby('AgeGroup').sum()
count = df_age.shape[0]
y = np.arange(1, 11)
labels = []
for i in range(count):
    age = df_age.index[i]
    labels.append(age)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.barh(y, list_male, tick_label=labels, label=' 男', color='#6699FF') 
ax.barh(y, list_female, tick_label=labels, label=' 女', color='#CC6699')
ax.set_ylabel("年龄段(岁)")
ax.set_xticks([-100000, -75000, -50000, -25000, 0, 25000, 50000, 75000, 100000])
ax.set_xticklabels(['100000', '75000', '50000', '25000', 
                    '0', '25000', '50000', '75000', '100000'])
ax.set_xlabel("人数(个)")
ax.set_title('某城市人口金字塔 41')
ax.legend()
plt.show()

  


7. Draw a funnel diagram:
The funnel diagram is also called an inverted triangle diagram, which presents the data in several stages, and the total proportion of each stage is 100%, and the data from one stage to another gradually decreases from top to bottom . The funnel diagram can help operators quickly find problems by displaying the changes in data at each stage of the business. It is suitable for process analysis scenarios with relatively standardized business processes, long cycles, and many links.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
num = 5
height = 0.5
x1 = np.array([1000, 500, 300, 200, 150])    
x2 = np.array((x1.max() - x1) / 2)
x3 = [i +j for i, j in zip(x1, x2)]
x3 = np.array(x3)
y = -np.sort(-np.arange(num))               
labels=['访问商品', '加购物车', '生成订单', '支付订单', '完成交易']
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
rects1 = ax.barh(y, x3, height, tick_label=labels, color='g', alpha=0.5)
rects2 = ax.barh(y, x2, height, color='w', alpha=1) 
ax.plot(x3, y, 'black', alpha=0.7)
ax.plot(x2, y, 'black', alpha=0.7)
notes = []
for i in range(0, len(x1)):
    notes.append('%.2f%%'%((x1[i] / x1[0]) * 100))
for rect_one, rect_two, note in zip(rects1, rects2, notes):
    text_x = rect_two.get_width() + (rect_one.get_width() - rect_two.get_width()) / 2 - 30
    text_y = rect_one.get_y() + height / 2
    ax.text(text_x, text_y, note, fontsize=12)
ax.set_xticks([])
for direction in ['top', 'left', 'bottom', 'right']:
    ax.spines[direction].set_color('none')
ax.yaxis.set_ticks_position('none')
plt.title('41')
plt.show()

  


8. Draw Sankey diagram:
Sankey diagram, also known as Sankey energy split diagram and Sankey energy balance diagram, is a specific type of flow chart used to show the "flow" changes of data. The Sankey diagram contains several branches extending from left to right. The width of each branch represents the size of the data flow, and the sum of the widths of all main branches is equal to the sum of the widths of all branches. It is common in scenarios such as energy, material composition or The financial sector. For example, the flow of energy in China in 2012 is shown in Figure 8-17.
The matplotlib.sankey module specifically provides the class Sankey representing the Sankey diagram. The Sankey diagram can be created by creating an object of the Sankey class, and then you can call the add) method to add some configuration options for the Sankey diagram, and finally call the finish() method to complete Sankey diagram drawing. The following will introduce the drawing process of the Sankey diagram step by step, the specific content is as follows.
1. Create a Sankey diagram
Matplotlib uses the construction method Sankey) to create a Sankey diagram.
2. Add options for Sankey diagram
Sankey class objects can call add) method to add data flow, labels and other options for Sankey diagram.
3. Return the Sankey diagram drawing completed object.
After adding data, the Sankey class object needs to call the finish) method to complete the drawing, and return a list containing multiple Sankey subgraphs. A Sankey subgraph contains the following fields.
· patch: Indicates the outline of a Sankey subgraph.
·flows: indicates flow value (input is positive, output is negative).
angles: A list representing the angles of the arrows.
Tips: an array representing the tip or trough position of the flow path, where each row is an (x, y).
·text: Indicates the Text instance of the center label.
· texts: Instances of Text representing stream branch labels.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
flows = [0.7, 0.3, -0.3, -0.1, -0.3, -0.1, -0.1, -0.1]
labels = ["工资", "副业", "生活", "购物", "深造", "运动", "其他", "买书"]
orientations = [1, 1, 0, -1, 1, -1, 1, 0]
sankey = Sankey()
sankey.add(flows=flows,                   
           labels=labels,                   
           orientations=orientations,       
           color="black",                   
           fc="lightgreen",                 
           patchlabel="生活消费 ",          
           alpha=0.7)                       
diagrams = sankey.finish()
diagrams[0].texts[4].set_color("r")         
diagrams[0].texts[4].set_weight("bold")     
diagrams[0].text.set_fontsize(20)           
diagrams[0].text.set_fontweight("bold")     
plt.title("日常生活开支的桑基图 41")
plt.show()

  

9. Draw a dendrogram:
a dendrogram, also known as a dendrogram, is a graph that describes the hierarchical structure of parent-child members through a tree structure. The shape of the dendrogram is generally an upside-down tree, the root of which is a root node without a parent member, and then connects the child members with a line from the root node, making the child members become child nodes, until the end of the line is no child. member's leaf node. Tree diagrams are used to illustrate the relationships and connections between members, and are often used in taxonomy, evolutionary science, business organization management, and other fields.

import pandas as pd
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as shc
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_excel(r'C:\Users\Administrator\Desktop\USArrests.xlsx')
plt.figure(figsize=(10, 6), dpi= 80)
plt.title("美国各州犯罪案件的树状图 41", fontsize=12)
dend = shc.dendrogram(shc.linkage(df[['Murder', 'Assault', 'UrbanPop']], method='ward'), 
                      labels=df.State.values, color_threshold=100)
plt.xticks(fontsize=10.5)
plt.ylabel('案例数量(个)')
plt.show()

 

10.: Draw a waffle chart:
A pie chart is also called a right-angle pie chart. It is a variant of a pie chart, which can visually show the proportion of a part to the whole. The waffle chart is generally composed of 100 squares, each of which represents 1%, and the different colors of the squares represent different categories. It is often used in the scene of comparing the completion ratio of the same type of indicators, such as movie attendance rate, company business actual Completion rate etc.

import matplotlib.pyplot as plt
from pywaffle import Waffle
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
plt.figure(FigureClass=Waffle, rows=10, columns=10, 
           values=[95, 5],vertical=True, colors=['#20B2AA', '#D3D3D3'], 
           title={'label': '41 电影《少年的你》上座率'}, 
           legend={'loc': 'upper right', 'labels': ['占座', '空座']}
)
plt.show()

  

————————————————
Copyright statement: This article is an original article of CSDN blogger "Zhi Huo", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this article for reprinting statement.
Original link: https://blog.csdn.net/m0_55680183/article/details/123915634

Guess you like

Origin blog.csdn.net/zy1620454507/article/details/128383554