python爬虫28 | 你爬下的数据不分析一波可就亏了啊,使用python进行数据可视化

    xiaoxiao2022-07-04  171

     

    通过这段时间

     

    小帅b教你从抓包开始

     

    到数据爬取

     

    到数据解析

     

    再到数据存储

     

    相信你已经能抓取大部分你想爬取的网站数据了

     

    恭喜恭喜

     

     

    但是

     

    数据抓取下来

     

    要好好分析一波

     

    最好的方式就是把数据进行可视化

     

    这样才能直观的感受到数据的魅力

     

    不过有一点

     

    现在市面上可以使用 python 的可视化库多如牛毛

     

    各有各的优点

     

    接下来小帅b把自己常用的一些可视化数据库分享给你

     

    好不?

     

     

    那么

     

    接下来就是

     

    学习 python 的正确姿势

     

     

    先来说说一个经典的可视化库

     

    matplotlib

     

    它是基于 NumPy 的一个数据可视化工具,内置了非常多图给我们使用

     

    接下来我们就来玩玩吧

     

    首先你得去下载一下这个库

     

       python -m pip install -U pip setuptoolspython -m pip install matplotlib

     

    下载完之后

     

    就可以来玩代码啦

     

    画画sin和cos线

     

                 import numpy as npimport .pyplot as plt x = np.linspace(-np.pi, np.pi, 256) cos = np.cos(x)sin = np.sin(x) plt.plot(x, cos, '--', linewidth=2)plt.plot(x, sin) plt.show()

     

     

    画个饼图

     

                # Pie chart, where the slices will be ordered and plotted counter-clockwise:labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'sizes = [15, 30, 45, 10]explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') fig1, ax1 = plt.subplots()ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.show()

     

     

    画画直方图

     

                          import numpy as npimport matplotlib.pyplot as plt np.random.seed(0) mu = 200sigma = 25x = np.random.normal(mu, sigma, size=100) fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4)) ax0.hist(x, 20, normed=1, histtype='stepfilled', facecolor='g', alpha=0.75)ax0.set_title('stepfilled') # Create a histogram by providing the bin edges (unequally spaced).bins = [100, 150, 180, 195, 205, 220, 250, 300]ax1.hist(x, bins, normed=1, histtype='bar', rwidth=0.8)ax1.set_title('unequal bins') fig.tight_layout()plt.show()

     

     

     

    更多关于 matplotlib 的文档可以到以下链接查看

     

    https://matplotlib.org/2.0.2/contents.html

     

     

    seaborn

     

    seaborn 是基于 matplotlib 的库,所以有更加高级的接口给我们使用,相对来说更加简单使用一些

     

    画个散点图

     

               import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snssns.set(style="darkgrid") tips = sns.load_dataset("tips")sns.relplot(x="total_bill", y="tip", data=tips);plt.show()

     

     

    画个折线图

     

        fmri = sns.load_dataset("fmri")sns.relplot(x="timepoint", y="signal", hue="event", kind="line", data=fmri);plt.show()

     

     

     

    画个直方图

     

          titanic = sns.load_dataset("titanic")sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic);plt.show()

     

     

     

    更多关于 seaborn 的可以看看以下链接

     

    https://seaborn.pydata.org/index.html

     

     

     

    pyecharts

     

    这是基于百度开源的数据可视化的 echarts 的库

     

    echarts 遇上了 python 之后

     

    就像巧克力遇上了音乐

     

    丝滑~

     

    特别是当 pyechart 结合  Notebook 的时候

     

    简直不能在丝滑了

     

    来画个直方图

     

                from pyecharts.charts import Barfrom pyecharts import options as opts bar = ( Bar() .add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"]) .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105]) .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49]) .set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况")))bar.render()

     

     

     

    画个饼图

     

                def pie_base() -> Pie: c = ( Pie() .add("", [list(z) for z in zip(Faker.choose(), Faker.values())]) .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例")) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) ) return c # 需要安装 snapshot_seleniummake_snapshot(driver, pie_base().render(), "pie.png")

     

     

     

     

    再来画个词云图

     

                                       words = [ ("Sam S Club", 10000), ("Macys", 6181), ("Amy Schumer", 4386), ("Jurassic World", 4055), ("Charter Communications", 2467), ("Chick Fil A", 2244), ("Planet Fitness", 1868), ("Pitch Perfect", 1484), ("Express", 1112), ("Home", 865), ("Johnny Depp", 847), ("Lena Dunham", 582), ("Lewis Hamilton", 555), ("KXAN", 550), ("Mary Ellen Mark", 462), ("Farrah Abraham", 366), ("Rita Ora", 360), ("Serena Williams", 282), ("NCAA baseball tournament", 273), ("Point Break", 265),] def wordcloud_base() -> WordCloud: c = ( WordCloud() .add("", words, word_size_range=[20, 100]) .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例")) ) return c # 需要安装 snapshot_seleniummake_snapshot(driver, wordcloud_base().render(), "WordCloud.png")

     

     

    是不是很丝滑

     

    更多关于 pyecharts 的可以到以下链接看看

     

    https://pyecharts.org

     

     

    好了

     

    以上就是小帅b常用到的几个可视化数据库

     

    当然

     

    还有很多可视化数据库

     

    不过这几个算是很友好的了

     

    希望对你有用

     

    那么

     

    我们下回见

     

    peace

     

     

    最新回复(0)