其他
数据呈现 | “Pandas”现在也可以绘制交互式的图形了,来看看怎么做的吧?
本文转载自公众号:关于数据分析与可视化
01 Plotly作为后端支持
在我们导入所需要用到的模块之后,我们需要导入进需要用到的数据库,并且添加下面这行代码,以激活“Plotly”作为后端的支持
import pandas as pd
import numpy as np
from sklearn.datasets import fetch_openml
pd.options.plotting.backend = 'plotly'
X,y = fetch_openml("wine", version=1, as_frame=True, return_X_y=True)
data = pd.concat([X,y], axis=1)
data.head()
fig = data[['Alcohol', 'Proline']].plot.scatter(y='Alcohol', x='Proline')
fig.show()
fig = data[['Hue', 'Proline', 'class']].plot.scatter(x='Hue', y='Proline', color='class', title='Proline and Hue by wine class')
fig.show()
data[['Hue','class']].groupby(['class']).mean().plot.bar()
02 Bokeh作为后端支持
pd.options.plotting.backend = 'pandas_bokeh'
import pandas_bokeh
from bokeh.io import output_notebook
from bokeh.plotting import figure, show
output_notebook()
p1 = data.plot_bokeh.scatter(x='Hue',
y='Proline',
category='class',
title='Proline and Hue by wine class',
show_figure=False)
show(p1)
output_notebook()
p1 = data.plot_bokeh.scatter(x='Hue',
y='Proline',
category='class',
title='Proline and Hue by wine class',
show_figure=False)
p2 = data[['Hue','class']].groupby(['class']).mean().plot.bar(title='Mean Hue per Class')
df_hue = pd.DataFrame({
'class_1': data[data['class'] == '1']['Hue'],
'class_2': data[data['class'] == '2']['Hue'],
'class_3': data[data['class'] == '3']['Hue']},
columns=['class_1', 'class_2', 'class_3'])
p3 = df_hue.plot_bokeh.hist(title='Distribution per Class: Hue')
df_proline = pd.DataFrame({
'class_1': data[data['class'] == '1']['Proline'],
'class_2': data[data['class'] == '2']['Proline'],
'class_3': data[data['class'] == '3']['Proline']},
columns=['class_1', 'class_2', 'class_3'])
p4 = df_proline.plot_bokeh.hist(title='Distribution per Class: Proline')
pandas_bokeh.plot_grid([[p1, p2],
[p3, p4]], plot_width=450)
今天的分享就到这里,希望大家看了有所收获!
星标⭐我们不迷路!想要文章及时到,文末“在看”少不了!
点击搜索你感兴趣的内容吧
往期推荐
数据Seminar
这里是大数据、分析技术与学术研究的三叉路口
推荐 | 张静红
欢迎扫描👇二维码添加关注