8个流行的Python可视化工具包,你喜欢哪个?
i小码哥
共 6990字,需浏览 14分钟
· 2021-02-26
![](https://filescdn.proginn.com/df923441abdd39a6a3ab87d6db37bb15/67b3b4ad13f5cc11baa0de79e08656fd.webp)
来源:机器之心 作者:Aaron Frederick
![](https://filescdn.proginn.com/df923441abdd39a6a3ab87d6db37bb15/67b3b4ad13f5cc11baa0de79e08656fd.webp)
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/3edc3be755c593b8f73339486f1b8f46/0846de37ff628986f098cfff9c11261f.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/7379bfcd81c062644780a77653128012/eb3d94cfae5ba4f13bde4252dd1b785f.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/792f94dbf19cdf5f39a64513dcaeb697/98da0968d3574476cd4b195c463371fa.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/3c370b88546adea6535434496553fc8d/1488e00847a9f75349b5607628a45b25.webp)
![](https://filescdn.proginn.com/6c4808d18cb7bcc6580c77a6f0470945/744d22397071edb0e83d1e663aa4b608.webp)
![](https://filescdn.proginn.com/31cdbe725ae7477655fba36ff7c39e94/d1068723e81f96e628109892143d6bc6.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/846b109c9d8306cb05ac94d56fd7f70e/0155cd8b98e6260c16b8418659a60d44.webp)
![](https://filescdn.proginn.com/3f42c9f782c14cb55911e8fb9c95cae2/c74ce145dae591885cdb2d7d3c1ce4ad.webp)
![](https://filescdn.proginn.com/79f13c2ca1bdd2216cd2f31332054c29/8e0dd76992a93bdc62e600937925d8b3.webp)
实例化图片;
用图片目标属性格式化;
用 figure.add() 将数据添加到图片中。
![](https://filescdn.proginn.com/6e7ca17032484414eb47ce1259dcb3d7/52495a628ec75875efa9d482d4d8990d.webp)
![](https://filescdn.proginn.com/d3e9ef30ac33ed91f9faff9500445c11/38d5da51a6f83c3e6b5c52377e5d5be6.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/cd96ee6d223ddbbed622f3960d73db08/48ac3667c6de90c50ef6a9c4c687ab0f.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/fc6b0d0a7b44466eccc0d8d8a602e718/3423f0c1f98e79c1ea4afa25073420a5.webp)
今天的文章就到这里,咋们下次再见!
👇👇各种Python案例,点击名片关注👇👇
评论