数据可视化——基于matplotlib库

以下示例都基于进行了import操作

import matplotlib.pyplot as plt

 

常用函数

  • plt.plot(x,y,, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12,alpha=0.2,label='line')

参数:自变量,因变量,颜色,点样式,线类型,线宽\点粗细,点大小,透明度(0到1变深),图例说明

color还可以是:归一化RGB元组(0.1,0.2,0.3),十六进制字符串‘#eeefff’

可通过help(plt.plot)获得帮助,比如plt.plot(x,y,'go'):‘go’代表绿色圆点

    =============    ===============================
    character        color
    =============    ===============================
    ``'b'``          blue
    ``'g'``          green
    ``'r'``          red
    ``'c'``          cyan
    ``'m'``          magenta
    ``'y'``          yellow
    ``'k'``          black
    ``'w'``          white
    =============    ===============================

    =============    ===============================
    character        description
    =============    ===============================
    ``'.'``          point marker
    ``','``          pixel marker
    ``'o'``          circle marker
    ``'v'``          triangle_down marker
    ``'^'``          triangle_up marker
    ``'<'``          triangle_left marker
    ``'>'``          triangle_right marker
    ``'1'``          tri_down marker
    ``'2'``          tri_up marker
    ``'3'``          tri_left marker
    ``'4'``          tri_right marker
    ``'s'``          square marker
    ``'p'``          pentagon marker
    ``'*'``          star marker
    ``'h'``          hexagon1 marker
    ``'H'``          hexagon2 marker
    ``'+'``          plus marker
    ``'x'``          x marker
    ``'D'``          diamond marker
    ``'d'``          thin_diamond marker
    ``'|'``          vline marker
    ``'_'``          hline marker
    =============    ===============================

    =============    ===============================
    character        description
    =============    ===============================
    ``'-'``          solid line style
    ``'--'``         dashed line style
    ``'-.'``         dash-dot line style
    ``':'``          dotted line style
    =============    ===============================
  • plt.title()

添加标题

  • plt.axis([xmin, xmax, ymin,ymax])

限定坐标轴范围

  • plt.xlabel('x')

横轴标签

  • plt.ylabel('y')

纵轴标签

  • plt.legend()

显示标签作为图例,需要在plt.plot中设置label

  • plt.grid(alpha=0.3)

显示网格

  • plt.annotate()

加注释

  • plt.style.use('ggplot')

设置作图风格,可通过print(plt.style.available)获得帮助

  • plt.subplot(224)

子图,2行2列第四个,后面常常跟plt.plot语句作图

  • plt.subplots

fig,ax = plt.subplots()等价于:

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

更多参见https://blog.csdn.net/xavier_muse/article/details/83859272

  • .fill

绘制填充图,下例分别用了subplots和subplot来实现fill操作

x=np.linspace(0,1,500)
y=np.sin(4*np.pi*x)*np.exp(-5*x)
#
# plt.subplot(111)
# plt.fill(x,y)

fig,ax=plt.subplots()
ax.fill(x,y)

plt.show()
  • plt.show()

展示图像

  • plt.ion()交互模式画多张图

https://blog.csdn.net/zbrwhut/article/details/80625702

结合pandas绘图

将numpy数据传入pandas.DataFrame,可便捷作图

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

DATA=np.random.randint(0,5,(10,4))
print(DATA)
df=pd.DataFrame(DATA,columns=['A','B','C','D'])

#折线图
#plt.plot(df) #方法一
# df.plot(alpha=1,linewidth=1.5)#方法二
#箱线图
# df.iloc[:,:].boxplot()
#直方图
# df.hist(bins=50)#bins调整柱间距离,各列分开绘制
# df.plot.bar()#各列绘制在一张图中
# df.plot.barh(stacked=True)#各列水平码放的柱状图
#散点图
df.plot.scatter(x='A',y="B",s=df['C']*100)#三个参数分别为x轴数据,y轴数据,点大小

plt.show()

除pandas外还可结合seaborn进行便捷作图

词云图

代码及相关资源:

https://github.com/shenxiangzhuang/PythonDataAnalysis/tree/master/Ch3Analysis-Visualization/Visualization

上面链接运行一下MwordClound.py即可

jieba是一个中文分词库,wordcloud库用来生成词云图

 

 

常见问题:

  • 在matplotlib.py中import matplotlib.pyplot as plt报错:

No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

cannot import name 'pyplot'

解决办法:将py文件改名

 

  • 中文显示

windows和linux的解决方法不同

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值