通過這段時間
小帥b教你從抓包開始
到數據爬取
到數據解析
再到數據存儲
相信你已經能抓取大部分你想爬取的網站數據了
恭喜恭喜
但是
數據抓取下來
要好好分析一波
最好的方式就是把數據進行可視化
這樣才能直觀的感受到數據的魅力
不過有一點
現在市面上可以使用 python 的可視化庫多如牛毛
各有各的優點
接下來小帥b把自己常用的一些可視化數據庫分享給你
好不?
那么
接下來就是
先來說說一個經典的可視化庫
matplotlib
它是基於 NumPy 的一個數據可視化工具,內置了非常多圖給我們使用
接下來我們就來玩玩吧
首先你得去下載一下這個庫
python -m pip install -U pip setuptools
python -m pip install matplotlib
下載完之后
就可以來玩代碼啦
畫畫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()
畫個餅圖
# 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()
更多關於 matplotlib 的文檔可以到以下鏈接查看
https://matplotlib.org/2.0.2/contents.html
seaborn
seaborn 是基於 matplotlib 的庫,所以有更加高級的接口給我們使用,相對來說更加簡單使用一些
畫個散點圖
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()
畫個折線圖
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 Bar
from 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_selenium
make_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_selenium
make_snapshot(driver, wordcloud_base().render(), "WordCloud.png")
是不是很絲滑
更多關於 pyecharts 的可以到以下鏈接看看
https://pyecharts.org
好了
以上就是小帥b常用到的幾個可視化數據庫
當然
還有很多可視化數據庫
不過這幾個算是很友好的了
希望對你有用
那么
我們下回見
peace