通過這段時(shí)間
小帥b教你從抓包開始
到數(shù)據(jù)爬取
到數(shù)據(jù)解析
再到數(shù)據(jù)存儲(chǔ)
相信你已經(jīng)能抓取大部分你想爬取的網(wǎng)站數(shù)據(jù)了
恭喜恭喜
但是
數(shù)據(jù)抓取下來(lái)
要好好分析一波
最好的方式就是把數(shù)據(jù)進(jìn)行可視化
這樣才能直觀的感受到數(shù)據(jù)的魅力
不過有一點(diǎn)
現(xiàn)在市面上可以使用 python 的可視化庫(kù)多如牛毛
各有各的優(yōu)點(diǎn)
接下來(lái)小帥b把自己常用的一些可視化數(shù)據(jù)庫(kù)分享給你
好不?
那么
接下來(lái)就是
學(xué)習(xí) python 的正確姿勢(shì)
先來(lái)說(shuō)說(shuō)一個(gè)經(jīng)典的可視化庫(kù)
matplotlib它是基于 NumPy 的一個(gè)數(shù)據(jù)可視化工具,內(nèi)置了非常多圖給我們使用
接下來(lái)我們就來(lái)玩玩吧
首先你得去下載一下這個(gè)庫(kù)
python -m pip install -U pip setuptools
python -m pip install matplotlib
下載完之后
就可以來(lái)玩代碼啦
畫畫sin和cos線
import numpy as np
import .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()
畫個(gè)餅圖
# 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 np
import matplotlib.pyplot as plt
np.random.seed(0)
mu = 200
sigma = 25
x = 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()
更多關(guān)于 matplotlib 的文檔可以到以下鏈接查看
https://matplotlib.org/2.0.2/contents.html
seaborn 是基于 matplotlib 的庫(kù),所以有更加高級(jí)的接口給我們使用,相對(duì)來(lái)說(shuō)更加簡(jiǎn)單使用一些
畫個(gè)散點(diǎn)圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")
tips = sns.load_dataset("tips")
sns.relplot(x="total_bill", y="tip", data=tips);
plt.show()
畫個(gè)折線圖
fmri = sns.load_dataset("fmri")
sns.relplot(x="timepoint", y="signal", hue="event", kind="line", data=fmri);
plt.show()
畫個(gè)直方圖
titanic = sns.load_dataset("titanic")
sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic);
plt.show()
更多關(guān)于 seaborn 的可以看看以下鏈接
https://seaborn.pydata.org/index.html
這是基于百度開源的數(shù)據(jù)可視化的 echarts 的庫(kù)
echarts 遇上了 python 之后
就像巧克力遇上了音樂
絲滑~
特別是當(dāng) pyechart 結(jié)合 Notebook 的時(shí)候
簡(jiǎn)直不能在絲滑了
來(lái)畫個(gè)直方圖
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["襯衫", "毛衣", "領(lǐng)帶", "褲子", "風(fēng)衣", "高跟鞋", "襪子"])
.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="某商場(chǎng)銷售情況"))
)
bar.render()
畫個(gè)餅圖
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=": {c}"))
)
return c
# 需要安裝 snapshot_selenium
make_snapshot(driver, pie_base().render(), "pie.png")
再來(lái)畫個(gè)詞云圖
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_selenium
make_snapshot(driver, wordcloud_base().render(), "WordCloud.png")
是不是很絲滑
更多關(guān)于 pyecharts 的可以到以下鏈接看看
https://pyecharts.org
好了
以上就是小帥b常用到的幾個(gè)可視化數(shù)據(jù)庫(kù)
當(dāng)然
還有很多可視化數(shù)據(jù)庫(kù)
不過這幾個(gè)算是很友好的了
希望對(duì)你有用
那么
我們下回見
peace
聯(lián)系客服