6种python数据可视化工具,python数据可视化工具包

这篇文章主要介绍了python数据可视化工具有哪些,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

Plotly官网
Plotly-express官网

1 Plotly简介

Plotly 是一个非常强大的开源数据可视化框架,它通过构建基于HTML的交互式图表来显示信息,可创建各种形式的精美图表。本文所说的Plotly指的是Plotly.js的Python封装,plotly本身是个生态非常复杂的绘图工具,它对很多编程语言提供接口Python中Turtle画蝴蝶。交互式和美观易用应该是Plotly最大的优势,而Matplotlib的特点则是可定制化程度高,但语法也相对难学,各有优缺点。
Plotly生态
(1)Plotly是绘图基础库,它可以深度定制调整绘图,但是API 复杂学习成本较高。
(2)Plotly_exprress则是对Plotly的高级封装,上手容易,它对 Plotly的常用绘图函数进行了封装。缺点是没有 plotly那样自由度高,个人感觉类似Seaborn和Matplotlib的关系。
(3)Dash用于创建交互式绘图工具,可以方便地用它来探索数据,其绘图基于Plotly。

2 Plotly-express

在这里插入图片描述
Plotly Express是Plotly库的内置部分,使用Plotly Express比直接调用graph object节省5到100倍的代码。
在这里插入图片描述

2.1 支持的数据格式

Input data arguments(参数) accepted by Plotly Express functions.Plotly Express提供了可视化各种类型数据的功能。

Column-oriented, Matrix or Geographic Data
Plotly Express适用于面向列、矩阵或地理数据。

(1)Most functions such as px.bar or px.scatter expect to operate on column-oriented data of the type you might store in a Pandas DataFrame (in either “long” or “wide” format, see below).
(2)px.imshow operates on matrix-like data you might store in a numpy or xarray array .
(3)px.choropleth and px.choropleth_mapbox can operate on geographic data of the kind you might store in a GeoPandas GeoDataFrame.
下面详细介绍如何为大多数Plotly Express函数提供面向列的数据。

Plotly Express works with Long-, Wide-, and Mixed-Form Data

存储面向列的数据有三种常见的约定,通常在具有列名的数据框中:
(1)long-form data has one row per observation, and one column per variable. This is suitable for storing and displaying multivariate data i.e. with dimension greater than 2. This format is sometimes called “tidy”.
长格式数据每个观察有一行,每个变量有一列。这适用于存储和显示多维数据,例如维度大于2的数据。这种格式有时被称为“tidy”。

import plotly.express as px
long_df = px.data.medals_long()
print(long_df)

总共观察了9次,相当于测量一次生成一条记录。
包含3个变量nation、medal、count。
在这里插入图片描述

(2)wide-form data has one row per value of one of the first variable, and one column per value of the second variable. This is suitable for storing and displaying 2-dimensional data.
宽格式数据在第一个变量的每个值上有一行,在第二个变量的每个值上有一列。这适用于存储和显示二维数据。

import plotly.express as px
wide_df = px.data.medals_wide()
print(wide_df)

第一个变量nation,包含3个值,所以有三行记录。
第二个变量medal,包含3个值,所以又有三列。
在这里插入图片描述

(3)mixed-form data is a hybrid of long-form and wide-form data, with one row per value of one variable, and some columns representing values of another, and some columns representing more variables.
混合表单数据是长表单和宽表单数据的混合体,一个变量的每个值一行,一些列表示另一个变量的值,一些列表示更多变量。

每个Plotly Express函数都可以对长格式数据进行操作(px.imshow除外,它只对宽格式输入进行操作)

2.1.1 Long-Form Input(多维)
import plotly.express as px
long_df = px.data.medals_long()

fig = px.bar(long_df,
             x="nation",
             y="count",
             color="medal",
             title="Long-Form Input")
fig.show()

请添加图片描述

2.1.2 Wide-Form Input(二维)
import plotly.express as px
wide_df = px.data.medals_wide()

fig = px.bar(wide_df,
             x="nation",
             y=["gold", "silver", "bronze"],
             title="Wide-Form Input")
fig.show()

请添加图片描述
您可能会注意到,第二个绘图的y轴和图例标签略有不同:它们分别是“value”和“variable”,这也反映在hoverlabel文本中。这是因为Plotly Express执行了内部Pandas melt()操作,将宽格式数据转换为长格式进行绘图,并使用Pandas约定为中间长格式数据指定列名。

请注意,“medal”和“count”标签不会出现在宽格式数据框中,因此在这种情况下,您必须自己提供这些标签,或者可以使用具有命名行和列索引的数据框。可以使用labels参数重命名这些标签:

import plotly.express as px
wide_df = px.data.medals_wide()

fig = px.bar(wide_df,
             x="nation",
             y=["gold", "silver", "bronze"],
             title="Wide-Form Input, relabelled",
            labels={"value": "count", "variable": "medal"})
fig.show()

请添加图片描述

2.1.3 Pandas DataFrames

As shown above, px functions supports natively pandas DataFrame. Arguments can either be passed as dataframe columns, or as column names if the data_frame argument is provided.
两种传参方式
一、Passing columns as arguments

import plotly.express as px
df = px.data.iris()
print(df.head())
# Use directly Columns as argument.
fig = px.scatter(df, 
                 x=df.sepal_length,
                 y=df.sepal_width,
                 color=df.species,
                 size=df.petal_length)
fig.show()

在这里插入图片描述
Long-Form Input(多维数据)
size参数控制数据点的大小
请添加图片描述
二、Passing name strings as arguments

import plotly.express as px
df = px.data.iris()
# Use column names instead.
fig = px.scatter(df,
                 x='sepal_length',
                 y='sepal_width',
                 color='species',
                 size='petal_length')
fig.show()

三、Using the index of a DataFrame
除了列之外,还可以将DataFrame的索引作为参数传递。在下面的示例中,索引显示在悬停(hover)数据中。

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df,
                 x=df.sepal_length,
                 y=df.sepal_width,
                 size=df.petal_length,
                 hover_data=[df.index])
fig.show()

请添加图片描述

2.1.4 array-like objects

px参数也可以是Long-Form或Wide-Form(对于某些函数)的类似数组的对象,如列表、NumPy数组。
一、List

import plotly.express as px

# List arguments
fig = px.line(x=[1, 2, 3, 4],
              y=[3, 5, 4, 8])
fig.show()

请添加图片描述
二、Numpy

import numpy as np
import plotly.express as px

t = np.linspace(0, 10, 100)
# NumPy arrays arguments
# override keyword names with labels
fig = px.scatter(x=t,
                 y=np.sin(t),
                 labels={'x':'t', 'y':'sin(t)'})
fig.show()

请添加图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值