第9章 可视化

本文深入介绍了Python中的数据可视化,包括matplotlib API的基础用法,如颜色、标记和线型设置,刻度、标签和图例的添加,以及注解和subplot绘图。同时,探讨了如何使用pandas和seaborn进行更高级的图表绘制,如线型图、柱状图、直方图、密度图和散步图。通过seaborn的set方法,可以轻松改变图形的外观,进一步增强视觉效果。最后,讲解了利用分面网格处理复杂类型数据的方法。
摘要由CSDN通过智能技术生成

9.1matplotlib API入门

#简单图形创建
import numpy as np
import matplotlib.pyplot as plt

data=np.arange(10)
data

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
plt.plot(data)
[<matplotlib.lines.Line2D at 0x1dc96438cd0>]

在这里插入图片描述

#创建figure
fig=plt.figure()

#创建子图
ax1=fig.add_subplot(2,2,1)#创建4个图,位置第一个
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)

ax3.plot(np.random.randn(50).cumsum(),'k--')
ax1.hist(np.random.randn(100),bins=20,color='b',alpha=0.2)
ax2.scatter(np.arange(30),np.arange(30)+3*np.random.randn(30))
<matplotlib.collections.PathCollection at 0x1dc96c17310>

在这里插入图片描述

#使用plt.subplots创建子图
fig,axes=plt.subplots(2,2,sharex=True,sharey=True)#axes是画图区域,figure是更大的画布

for i in range(2):
    for j in range(2):
        axes[i,j].hist(np.random.randn(500),bins=50,color='r')
        
plt.subplots_adjust(wspace=0.1,hspace=0.1)#subplots_adjust用于调整子图

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

颜色、标记和线型

对于字符串格式,标记类型和线型必须放在颜色后面,如’ro–’

plt.subplots(1,1)
plt.plot(np.random.randn(30),'ro--')

#另一种形式
plt.plot(np.random.randint(0,3,100),'k^--')
plt.show()

在这里插入图片描述

#修改插值方式
data=np.random.randn(30).cumsum()

plt.subplots(1,1)
plt.plot(data,'bo--',label='default')
plt.plot(data,'yo--',drawstyle='steps-post',label='steps-post')

plt.legend(loc='best')
<matplotlib.legend.Legend at 0x1dc96faa340>

在这里插入图片描述

刻度、标签和图例
fig=plt.figure()

ax=fig.add_subplot(1,1,1)

ax.plot(np.random.randn(1000).cumsum())

#设置刻度
ticks=ax.set_xticks([0,250,500,750,1000]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值