python100天入门教程视频 (python 100 days视频)

前言 英文资料的学习

Stacked bar charts can be used to visualize discrete distributions.

This example visualizes the result of a survey in which people could rate their agreement to questions on a five-element scale.

The horizontal stacking is achieved by calling barh() for each category and passing the starting point as the cumulative sum of the already drawn bars via the parameter left.

Stacked :原型 stack 栈 ,原义是 草堆,我们在放置东西的时候是从下向上放,拿的时候从上入下拿,这个与放置的顺序恰好相反。所在在计算机中有个专门 的词 stack 栈,后进先出

visualize :动词 可视化,使...可视化。形容词visual 例如我们用于是的vc++ vscode 的这个v 就是visual这个单词

discret : disconnect connect 我们可以猜出dis的意义其实是分离的意味。这里是独立部件

下面有一个例子显示不同问题的得分,每个问题的不同维度的得分又是不一样的

项调查的结果,在该调查中,人们可以在五要素量表上对他们对问题的同意进行评分。

水平堆叠是通过为每个类别调用 barh() 并通过参数 left 将起点作为已绘制柱的累积总和传递来实现的

python49天参数怎么设置,python100天入门教程视频

上图中有6个问题每个问题在不同维度的得分不一样,通过不同颜色标识

先看代码,然后在分析

import numpy as np
import matplotlib.pyplot as plt


category_names = ['Strongly disagree', 'Disagree',
                  'Neither agree nor disagree', 'Agree', 'Strongly agree']
results = {
    'Question 1': [10, 15, 17, 32, 26],
    'Question 2': [26, 22, 29, 10, 13],
    'Question 3': [35, 37, 7, 2, 19],
    'Question 4': [32, 11, 9, 15, 33],
    'Question 5': [21, 29, 5, 5, 40],
    'Question 6': [8, 19, 5, 30, 38]
}


def survey(results, category_names):

    labels = list(results.keys())
    data = np.array(list(results.values()))
    data_cum = data.cumsum(axis=1)
    category_colors = plt.get_cmap('RdYlGn')(
        np.linspace(0.15, 0.85, data.shape[1]))

    fig, ax = plt.subplots(figsize=(9.2, 5))
    ax.invert_yaxis()
    ax.xaxis.set_visible(False)
    ax.set_xlim(0, np.sum(data, axis=1).max())

    for i, (colname, color) in enumerate(zip(category_names, category_colors)):
        widths = data[:, i]
        starts = data_cum[:, i] - widths
        ax.barh(labels, widths, left=starts, height=0.5,
                label=colname, color=color)
        xcenters = starts + widths / 2

        r, g, b, _ = color
        text_color = 'white' if r * g * b < 0.5 else 'darkgrey'
        for y, (x, c) in enumerate(zip(xcenters, widths)):
            ax.text(x, y, str(int(c)), ha='center', va='center',
                    color=text_color)
    ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1),
              loc='lower left', fontsize='small')

    return fig, ax


survey(results, category_names)
plt.show()

代码分析

定义问题与得分的关系

使用一个 dic 保存问题与分数的关系,每个元素是 str -> list

results = {
'Question 1': [10, 15, 17, 32, 26],
'Question 2': [26, 22, 29, 10, 13],
'Question 3': [35, 37, 7, 2, 19],
'Question 4': [32, 11, 9, 15, 33],
'Question 5': [21, 29, 5, 5, 40],
'Question 6': [8, 19, 5, 30, 38]
}

定义不同维度的名字是一个list长度与分数个数保持一致

category_names = ['Strongly disagree', 'Disagree',
'Neither agree nor disagree', 'Agree', 'Strongly agree']

labels 是什么,一个list

['Question 1', 'Question 2', 'Question 3', 'Question 4', 'Question 5', 'Question 6']

data = np.array(list(results.values()))

[
  [10 15 17 32 26]
[26 22 29 10 13]
[35 37 7 2 19]
[32 11 9 15 33]
[21 29 5 5 40]
[ 8 19 5 30 38]
]

np.cumsum(arr, axis=0)和arr.cumsum(axis=0)是一样的。按照轴0进行累计求和的。

指定参数axis=1

>>> arr
 array([[0, 1, 2],
         [3, 4,   5],
         [6, 7, 8]])
  >>> arr.cumsum(axis=1)
array([[ 0,  1,  3],
         [ 3,   7, 12],
         [ 6, 13, 21]], dtype=int32)

我们可以发现纵轴方向的每一列的元素等于前面n列的元素之和,相当于是替换了每一旬元素值 (等于前面列一直加到 当前列的值,作为当前列的新值)

0 1, 2 --> 3

3, 4, 5 --> 12

6, 7, 8 --》 21

data_cum = data.cumsum(axis=1)

大家可以根据 cumsum函数的作用自行算出 data_cum数组的值

data_cum = data.cumsum(axis=1)


data的值

[[10 15 17 32 26]
 [26 22 29 10 13]
 [35 37  7  2 19]
 [32 11  9 15 33]
 [21 29  5  5 40]
 [ 8 19  5 30 38]]

data_cum的值.

[[ 10  25  42  74 100]
 [ 26  48  77  87 100]
 [ 35  72  79  81 100]
 [ 32  43  52  67 100]
 [ 21  50  55  60 100]
 [  8  27  32  62 100]]

print(data.shape) 表示的是一个张量,在我看来其实是一个数组的维度

print(data.shape)
(6, 5)

表示这个data是一个 6行5列,是每一行有5个元素
print(data.shape[1])

结果是 5 
5
category_colors = plt.get_cmap('RdYlGn')(
np.linspace(0.15, 0.85, data.shape[1]))

本质上是对某个颜色作了渐变

python49天参数怎么设置,python100天入门教程视频

如何画出连续的不同区域的值

widths = data[:, i]
starts = data_cum[:, i] - widths
ax.barh(labels, widths, left=starts, height=0.5,
label=colname, color=color)

starts = data_cum[:, i] - widths 起始点

widths :代表表实际要画的长度。

大家可以思考前面 data_cum计算逻辑,为了使不同区间的数字连续,实际上data_cum的值其实是进行了扩容的。

未完待续