-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatplotlib
More file actions
146 lines (115 loc) · 5.15 KB
/
Copy pathmatplotlib
File metadata and controls
146 lines (115 loc) · 5.15 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
http://matplotlib.org/users/screenshots.html
matplotlib绘图可视化知识点整理 http://blog.csdn.net/panda1234lee/article/details/52311593
echart画图 https://zhuanlan.zhihu.com/p/27990382
===
# 雷达图
from pyecharts import Radar
v1 = [list(radar_d.loc['客户群1',:])]
v2 = [list(radar_d.loc['客户群2',:])]
v3 = [list(radar_d.loc['客户群3',:])]
v4 = [list(radar_d.loc['客户群4',:])]
v5 = [list(radar_d.loc['客户群5',:])]
c_schema= [{"name": "L", "max": 2.5, "min": -1},
{"name": "R", "max": 2.5, "min": -1},
{"name": "F", "max": 2.5, "min": -1},
{"name": "M", "max": 2.5, "min": -1},
{"name": "C", "max": 2.5, "min": -1}]
radar = Radar()
radar.config(c_schema=c_schema, shape='circle')
radar.add('客户群1', v1, item_color="#ff0000", symbol=None)
radar.add('客户群2', v2, item_color="#477725", symbol=None)
radar.add('客户群3', v3, item_color="#66ff00", symbol=None)
radar.add('客户群4', v4, item_color="#66ffff", symbol=None)
radar.add('客户群5', v5, item_color="#FFFF00", symbol=None)
radar.show_config()
# radar.render() # 在HTML中显示出来
radar # 在notebook中显示
# 帕累托图
dish_profit = '数据源\\图书配套数据、代码\chapter3\demo\data\catering_dish_profit.xls' #餐饮菜品盈利数据
d = pd.read_excel(dish_profit, index_col = u'菜品名')#构造dataframe结构,dataframe是表格型数据结构,index_col = u'菜品名'设定索引
s = d[u'盈利']#此时的data是series数据结构,获得盈利数据
import matplotlib.pyplot as plt #导入图像库
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
plt.figure()
s.plot(kind='bar')#设置图像类型 kind='bar'
plt.ylabel(u'盈利(元)')
p = 1.0*s.cumsum()/s.sum()#cumsum()是累计和
p.plot(color = 'r', secondary_y = True, style = '-o',linewidth = 3)#secondary_y第二个y轴,style = '-o'指点的类型,linewidth线的粗细
plt.ylabel(u'盈利(比例)')
plt.annotate(format(p[6], '.4%'), xy = (6, p[6]), xytext=(6*0.9, p[6]*0.9), arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.show()
# 饼图
labels = 'a','b','c'
sizes = [10,20,70]
colors = ['red','green','blue']
explode = (0,0,0.1)
plt.pie(sizes,explode =explode,labels=labels,colors=colors,autopct = '%1.1f%%',shadow=True, startangle=10)
plt.axis('equal')
plt.show()
#箱线图
import pandas as pd
catering_sale = '数据源\\图书配套数据、代码\chapter3\demo\data\catering_sale.xls'
data = pd.read_excel(catering_sale, index_col = u'日期')#将日期作为索引列
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']#用来显示中文标签
plt.rcParams['axes.unicode_minus'] = False#用来显示负号
plt.figure(figsize=(7,5))#可以指定图像区域的大小
data.boxplot()
p = data.boxplot(return_type='dict')#获得数据
x = p['fliers'][0].get_xdata()#get_xdata()获得异常值,x的数据类型为array
y = p['fliers'][0].get_ydata()#y的数据类型为array
y.sort()
for i in range(len(x)):#annotate用来添加注释,放置注释的位置
if i > 0:
plt.annotate(y[i], xy = (x[i], y[i]), xytext = (x[i] + 0.05 - 0.8 / (y[i]- y[i - 1]), y[i]))
else:
plt.annotate(y[i], xy = (x[i], y[i]), xytext =(x[i] + 0.08, y[i]))
plt.show()
# 问题
plt.colorbar()
# 常见画图
折线图
x = np.linspace(1,2*np.pi, num = 100)
y = np.sin(x)
plt.plot(x,y)
柱状图。x y 需一一对应
x = np.arange(len(y))
y = np.random.randint(0,9,size=(4))
plt.bar(x,y)
plt.barh(x,y) # 条形图
多维柱状图
x = np.arange(len(y))
y = np.random.randint(0,9,size=(3,3))
plt.bar(x, y[0],color='r',width = 0.25)
plt.bar(x+0.25, y[1],color='b',width = 0.25)
plt.bar(x+0.5, y[2],color='y',width = 0.25)
堆积柱状图
x = np.arange(len(y))
y = np.random.randint(0,9,size=(3,3))
plt.bar(x,y[0],color = 'r',width = 0.25)
plt.bar(x,y[1], bottom = y[0],color = 'y',width = 0.25)
plt.bar(x,y[2], bottom = y[0]+y[1],color = 'b',width = 0.25)
直方图 plt.hist(x,10)
#
引入模块 正常显示中文
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
使用R语言中的ggplot2配色作为绘图风格,纯粹为了好看 plt.style.use('ggplot')
%matplotlib inline 可以省略 plt.show()
文本注释
s.plot(color = 'r', style = '-o', secondary_y = True, linewidth = 3)
plt.annotate(p[6],xy = (6, p[6]), xytext=(6*0.9, p[6]*0.9), arrowprops=dict(arrowstyle="->"))
p[6]需要显示的文本内容,xy需要显示文本内容的点,xytext文本内容的位置,arrowprops箭头指向
多重图像
plt.subplot(121) 表示1行两列,下面的图表放在第一列
d2.groupby('user_id')['order_amount'].sum().hist(bins=300)
plt.subplot(122) 表示1行两列,下面的图表放在第二列
d2.groupby('user_id')['order_products'].sum().hist(bins=300)
设定坐标轴的范围值:plt.ylim(0,1.05)
设置图例的位置:plt.legend(loc=8)
设置刻度标签 plt.ylabel('rio')
多个图画到一个表格subplot
matshow Plot a matrix or array as an image http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.matshow.html?