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

·Artist

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

2、Artist有两种类型:primitivescontainers: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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值