8 个流行的 Python 可视化工具包,你喜欢哪个?
数据分析1480
共 6962字,需浏览 14分钟
· 2020-09-08
![](https://filescdn.proginn.com/555597c905be59aa7f272d1f0a45a453/60861ed3d4f97f7feb6a8d2bc3e8cd8e.webp)
作者 | Aaron Frederick 参与 | 李诗萌、王淑婷
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/e5fbaafe430aa9d8e5cc2bda914c6ace/5558f9144b9e48a5c7a21c2a675c67af.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/d0cf1014f37620216c8132310fa27ec2/b54002a208a50604e87db7ff9a0b75b7.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/ad703ae6e1ffa222606273b1da887e12/8335c45d01ddf0558da1e7f11fddc08f.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/86e360546b6ba8d3842e215530a0da45/fa5da813359a37ede1fd75028b936f7b.webp)
![](https://filescdn.proginn.com/6a9de391da5ce85935a5e34bc0ec4470/72ebec2cdffb4432f8187b5a2bdc65a9.webp)
![](https://filescdn.proginn.com/f1202775a44f3a9ec23879f11a682db8/ea4b4aa5c140f1881b14e70fcd75f5d8.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/01a6a14de24984570d54a15fb816d781/31196b7c9ceecfa7a569b826a294cb87.webp)
![](https://filescdn.proginn.com/c9a80dde2f8ddaf0fbc24e2f634ac2c1/96105f60dbfe81a95a82244baccb8eb2.webp)
![](https://filescdn.proginn.com/8edb349d5d99b96ffa73a7889454e78c/23a54d3e90ac66dd563352ebe3830c16.webp)
实例化图片;
用图片目标属性格式化;
用 figure.add() 将数据添加到图片中。
![](https://filescdn.proginn.com/975e94a202bc225d820a48b330fd7ab8/dc5e00069bf975acd61fd43bb1bc4a62.webp)
![](https://filescdn.proginn.com/3491817a9018c79897ac29b1f3e04ade/d251c318424fc34c6ac30f06fe624be1.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/8177ae3daa74e15ec2ec15a3a848bc92/af45ebba36a17aba7788c2410c388ae2.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/043bf1819b435c89c5538fa08422a236/2265ff701bf5b597d87ecf736509834b.webp)
- 完 -
评论