thoughts about a python program

My Thoughts about a Python Program

There is a picture in the news from 2018. We can try to draw it using Matplotlib.

The picture can be found from: https://verimake.com/topics/96

I learnt how to do it from the official website of Matplotlib:

https://matplotlib.org/gallery/lines_bars_and_markers/horizontal_barchart_distribution.html#sphx-glr-gallery-lines-bars-and-markers-horizontal-barchart-distribution-py

steps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#the preparation
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # make Chinese can be shown
plt.rcParams['axes.unicode_minus'] = False # make '-' can be shown

#create dataset
giants = ['腾讯系', '头条系', '百度系', '阿里系', '新浪系', '其它']
ratios = {'2017年6月' : [54.3, 3.9, 7.9, 6.7, 2.9, 24.3],
'2018年6月' : [47.7, 10.1, 7.4, 6.4, 3.6, 24.8]}

#put data in right form
import pandas as pd
data=pd.DataFrame({"2017":ratios['2017年6月'],"2018":ratios['2018年6月']},index=giants)


#the next step is to use 'plt.barh()'. In order to use it, I use the codes from its official website
def survey(results, category_names):
"""
Parameters
----------
results : dict
A mapping from question labels to a list of answers per category.
It is assumed all lists contain the same number of entries and that
it matches the length of *category_names*.
category_names : list of str
The category labels.
"""
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, 2))
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(c)+"%", ha='center', va='center',
color="black")
j=0
for x in range(6):
a=(data_cum[0][x]+data_cum[1][x])/2-(data[0][x]+data[1][x])/4
ax.text(a,0.5,change[j], ha='center', va='center',
color="black")
j=j+1
ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1),
loc='lower left', fontsize='small')

return fig, ax


survey(ratios, giants)
plt.show()

Finally, I plot the picture successfully.

y2T5qJ.png

During the learning, I changed variables in the example codes and see whether there is a change in the picture plotted, so that I can understand the mean of different parameters. In addition, I can try the codes that I had learnt before in the new program. Therefore, I can both consolidate old knowledge and learn new ones.

This activity also improved my ability of self-learning, as I needed to read and understand the codes and explanation by myself. I really got a sense of achievement after this program.