栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数据可视化学习笔记—绘图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据可视化学习笔记—绘图

·Artist

1、matplotlib的原理或者说基础逻辑是,用Artist对象在画布(canvas)上绘制(Render)图形。

2、Artist有两种类型:primitives 和containers:primitive是基本要素,container是容器

·绘制折线图lines

1、 plot方法绘制

x = range(0,5)

y1 = [2,5,7,8,10]

y2= [3,6,8,9,11]

fig,ax= plt.subplots()

ax.plot(x,y1)

ax.plot(x,y2);

print(ax.lines)

2、用Line2D对象绘制

x = range(0,5)
y1 = [2,5,7,8,10]
y2= [3,6,8,9,11]
fig,ax= plt.subplots()
lines = [Line2D(x, y1), Line2D(x, y2,color='orange')]
for line in lines:
    ax.add_line(line)
ax.set_xlim(0,4)
ax.set_ylim(2, 11);
·绘制直方图

1、hist绘制

x=np.random.randint(0,100,100)
bins=np.arange(0,101,10)
plt.hist(x,bins,color='fuchsia',alpha=0.5) 
plt.xlabel('scores') 
plt.ylabel('count') 
plt.xlim(0,100);
plt.show()

2、rectangle矩形类绘制

​df = pd.DataFrame(columns = ['data'])
df.loc[:,'data'] = x
df['fenzu'] = pd.cut(df['data'], bins=bins, right = False,include_lowest=True)
df_cnt = df['fenzu'].value_counts().reset_index()
df_cnt.loc[:,'mini'] = df_cnt['index'].astype(str).map(lambda x:re.findall('[(.*),',x)[0]).astype(int)
df_cnt.loc[:,'maxi'] = df_cnt['index'].astype(str).map(lambda x:re.findall(',(.*))',x)[0]).astype(int)
df_cnt.loc[:,'width'] = df_cnt['maxi']- df_cnt['mini']
df_cnt.sort_values('mini',ascending = True,inplace = True)
df_cnt.reset_index(inplace = True,drop = True)
fig = plt.figure()
ax1 = fig.add_subplot(111)
for i in df_cnt.index:
   rect=plt.Rectangle((df_cnt.loc[i,'mini'],0),df_cnt.loc[i,'width'],df_cnt.loc[i,'fenzu'])
   ax1.add_patch(rect)
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 16);
·绘制柱状图

1、bar绘制

y = range(1,17)
plt.bar(np.arange(16), y, alpha=0.5, width=0.5, color='yellow', edgecolor='red', label='The First Bar', lw=3);

2、Rectangle矩形类绘制

fig = plt.figure()
ax1 = fig.add_subplot(111)
for i in range(1,17):
    rect =  plt.Rectangle((i+0.25,0),0.5,i)
    ax1.add_patch(rect)
ax1.set_xlim(0, 16)
ax1.set_ylim(0, 16);
·绘制饼状图

1、pie绘制饼状图

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10] 
explode = (0, 0.1, 0, 0) 
fig1, ax1 = plt.subplots() 
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) 
ax1.axis('equal'); 

2、wedge绘制饼状图

fig = plt.figure(figsize=(5,5))
ax1 = fig.add_subplot(111)
theta1 = 0
sizes = [15, 30, 45, 10] 
patches = []
patches += [
    Wedge((0.5, 0.5), .4, 0, 54),           
    Wedge((0.5, 0.5), .4, 54, 162),  
    Wedge((0.5, 0.5), .4, 162, 324),           
    Wedge((0.5, 0.5), .4, 324, 360),  
]
colors = 100 * np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.8)
p.set_array(colors)
ax1.add_collection(p);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号