8个流行的Python可视化工具包。
机器学习算法与Python实战
共 7183字,需浏览 15分钟
· 2020-07-28
(给机器学习算法与Python实战加星标,提升AI技能)
import seaborn as sns
import matplotlib.pyplot as plt
color_order = ['xkcd:cerulean', 'xkcd:ocean',
'xkcd:black','xkcd:royal purple',
'xkcd:royal purple', 'xkcd:navy blue',
'xkcd:powder blue', 'xkcd:light maroon',
'xkcd:lightish blue','xkcd:navy']
sns.barplot(x=top10.Team,
y=top10.Salary,
palette=color_order).set_title('Teams with Highest Median Salary')
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
![](https://filescdn.proginn.com/cb2890d44c9b17d508108dfb2862ecfb/2ee51ed2ce665bdab6b40dde6ca51510.webp)
import matplotlib.pyplot as plt
import scipy.stats as stats
#model2 is a regression model
log_resid = model2.predict(X_test)-y_test
stats.probplot(log_resid, dist="norm", plot=plt)
plt.title("Normal Q-Q plot")
plt.show()
![](https://filescdn.proginn.com/f17e7223899a8b4f2dd500f8fdafe98d/0f8be7ac7cf81b733a96e87a5da16f7f.webp)
#All Salaries
ggplot(data=df, aes(x=season_start, y=salary, colour=team)) +
geom_point() +
theme(legend.position="none") +
labs(title = 'Salary Over Time', x='Year', y='Salary ($)')
![](https://filescdn.proginn.com/55072d95a621923d4db71614db8e453f/c2cd3a23fa0fafd2b7c26caa0baf90f8.webp)
import pandas as pd
from bokeh.plotting import figure
from bokeh.io import show
# is_masc is a one-hot encoded dataframe of responses to the question:
# "Do you identify as masculine?"
#Dataframe Prep
counts = is_masc.sum()
resps = is_masc.columns
#Bokeh
p2 = figure(title='Do You View Yourself As Masculine?',
x_axis_label='Response',
y_axis_label='Count',
x_range=list(resps))
p2.vbar(x=resps, top=counts, width=0.6, fill_color='red', line_color='black')
show(p2)
#Pandas
counts.plot(kind='bar')
![](https://filescdn.proginn.com/f61850faac470caa786f7c462d853dea/1e7e2520f5e7d09d06b4d30e31705c8a.webp)
![](https://filescdn.proginn.com/31daa1cf1d0dbb7a2aa668b949b405b0/d3702186a3f6eb2793af805c4ad30dcd.webp)
安装时要有 API 秘钥,还要注册,不是只用 pip 安装就可以;
Plotly 所绘制的数据和布局对象是独一无二的,但并不直观;
图片布局对我来说没有用(40 行代码毫无意义!)
你可以在 Plotly 网站和 Python 环境中编辑图片;
支持交互式图片和商业报表;
Plotly 与 Mapbox 合作,可以自定义地图;
很有潜力绘制优秀图形。
#plot 1 - barplot
# **note** - the layout lines do nothing and trip no errors
data = [go.Bar(x=team_ave_df.team,
y=team_ave_df.turnovers_per_mp)]
layout = go.Layout(
title=go.layout.Title(
text='Turnovers per Minute by Team',
xref='paper',
x=0
),
xaxis=go.layout.XAxis(
title = go.layout.xaxis.Title(
text='Team',
font=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
),
yaxis=go.layout.YAxis(
title = go.layout.yaxis.Title(
text='Average Turnovers/Minute',
font=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
),
autosize=True,
hovermode='closest')
py.iplot(figure_or_data=data, layout=layout, filename='jupyter-plot', sharing='public', fileopt='overwrite')
#plot 2 - attempt at a scatterplot
data = [go.Scatter(x=player_year.minutes_played,
y=player_year.salary,
marker=go.scatter.Marker(color='red',
size=3))]
layout = go.Layout(title="test",
xaxis=dict(title='why'),
yaxis=dict(title='plotly'))
py.iplot(figure_or_data=data, layout=layout, filename='jupyter-plot2', sharing='public')
[Image: image.png]
![](https://filescdn.proginn.com/110197d957b0a084e74f20dc0a4655c8/d00211b9f914881ccda3b08fa1f4c086.webp)
![](https://filescdn.proginn.com/30413ff7d661428c9b739cd0eafa3a10/f238a470c24a816e8c7f6ea7eaf6b948.webp)
![](https://filescdn.proginn.com/976ede1150f246c564f06f43e7fcce8d/db32ca272f651688838046f475db66f8.webp)
实例化图片;
用图片目标属性格式化;
用 figure.add() 将数据添加到图片中。
![](https://filescdn.proginn.com/b6740fe6acc7817ac9eac950529c394f/88492de9914680348e535e1b35aa19ea.webp)
![](https://filescdn.proginn.com/4452c4d8c7b6affc30d8bcf5a6de1867/5ef3477138fd66411c8a95090efba86f.webp)
options = {
'node_color' : range(len(G)),
'node_size' : 300,
'width' : 1,
'with_labels' : False,
'cmap' : plt.cm.coolwarm
}
nx.draw(G, **options)
![](https://filescdn.proginn.com/fd60587642ac8f8da5e43ebde45e2017/6d7015dc53ce555c49d95c899cc7d311.webp)
import itertools
import networkx as nx
import matplotlib.pyplot as plt
f = open('data/facebook/1684.circles', 'r')
circles = [line.split() for line in f]
f.close()
network = []
for circ in circles:
cleaned = [int(val) for val in circ[1:]]
network.append(cleaned)
G = nx.Graph()
for v in network:
G.add_nodes_from(v)
edges = [itertools.combinations(net,2) for net in network]
for edge_group in edges:
G.add_edges_from(edge_group)
options = {
'node_color' : 'lime',
'node_size' : 3,
'width' : 1,
'with_labels' : False,
}
nx.draw(G, **options)
![](https://filescdn.proginn.com/955be265ddf454cca14e45b912e762a2/4919cdd977eccc9c4fe548ad9df94e97.webp)
推荐阅读
(点击标题可跳转阅读)
学深度学习是不是需要先学机器学习? 卧槽,我学会了用Python预测股票价格
老铁,三连支持一下,好吗?↓↓↓
评论