音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

关于Python可视化Dash工具之plotly基本图形示例详解
日期:2021-09-08 13:51:44   来源:脚本之家

Plotly Express是对 Plotly.py 的高级封装,内置了大量实用、现代的绘图模板,用户只需调用简单的API函数,即可快速生成漂亮的互动图表,可满足90%以上的应用场景。

本文借助Plotly Express提供的几个样例库进行散点图、折线图、饼图、柱状图、气泡图、桑基图、玫瑰环图、堆积图、二维面积图、甘特图等基本图形的实现。

代码示例

import plotly.express as px
df = px.data.iris()
#Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species','species_id'],dtype='object')
#   sepal_length sepal_width ...  species species_id
# 0       5.1     3.5 ...   setosa      1
# 1       4.9     3.0 ...   setosa      1
# 2       4.7     3.2 ...   setosa      1
# ..      ...     ... ...    ...     ...
# 149      5.9     3.0 ... virginica      3
# plotly.express.scatter(data_frame=None, x=None, y=None, 
# color=None, symbol=None, size=None,
# hover_name=None, hover_data=None, custom_data=None, text=None,
# facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None,
# error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,
# animation_frame=None, animation_group=None,
# category_orders=None, labels=None, orientation=None,
# color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, 
# range_color=None, color_continuous_midpoint=None,
# symbol_sequence=None, symbol_map=None, opacity=None, 
# size_max=None, marginal_x=None, marginal_y=None,
# trendline=None, trendline_color_override=None, 
# log_x=False, log_y=False, range_x=None, range_y=None,
# render_mode='auto', title=None, template=None, width=None, height=None)
# 以sepal_width,sepal_length制作标准散点图
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()
 
 
#以鸢尾花类型-species作为不同颜色区分标志 color
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
 
#追加petal_length作为散点大小,变位气泡图 size
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         color="species",size='petal_length')
fig.show()
 
#追加petal_width作为额外列,在悬停工具提示中显示为额外数据 hover_data
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         color="species", size='petal_length',
         hover_data=['petal_width'])
fig.show()
 
#以鸢尾花类型-species区分散点的形状 symbol
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", 
         size='petal_length',
         hover_data=['petal_width'])
fig.show()
 
#追加petal_width作为额外列,在悬停工具提示中以粗体显示。 hover_name
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", 
         size='petal_length',
         hover_data=['petal_width'], hover_name="species")
fig.show()
 
#以鸢尾花类型编码-species_id作为散点的文本值 text
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", 
         size='petal_length',
         hover_data=['petal_width'], hover_name="species",
         text="species_id")
fig.show()
 
#追加图表标题 title
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", size='petal_length',
         hover_data=['petal_width'], hover_name="species",
         text="species_id",title="鸢尾花分类展示")
fig.show()
 
#以鸢尾花类型-species作为动画播放模式 animation_frame
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", size='petal_length',
         hover_data=['petal_width'], hover_name="species",
         text="species_id",title="鸢尾花分类展示",
         animation_frame="species")
fig.show()
 
#固定X、Y最大值最小值范围range_x,range_y,防止动画播放时超出数值显示
fig = px.scatter(df, x="sepal_width", y="sepal_length",
         symbol="species" ,color="species", size='petal_length',
         hover_data=['petal_width'], hover_name="species",
         text="species_id",title="鸢尾花分类展示",
         animation_frame="species",range_x=[1.5,4.5],range_y=[4,8.5])
fig.show()
 
df = px.data.gapminder().query("country=='China'")
# Index(['country', 'continent', 'year', 'lifeExp', 'pop', 'gdpPercap', 'iso_alpha', 'iso_num'],dtype='object')
#   country continent year ...  gdpPercap iso_alpha iso_num
# 288  China   Asia 1952 ...  400.448611    CHN   156
# 289  China   Asia 1957 ...  575.987001    CHN   156
# 290  China   Asia 1962 ...  487.674018    CHN   156
# plotly.express.line(data_frame=None, x=None, y=None, 
# line_group=None, color=None, line_dash=None,
# hover_name=None, hover_data=None, custom_data=None, text=None,
# facet_row=None, facet_col=None, facet_col_wrap=0, 
# facet_row_spacing=None, facet_col_spacing=None,
# error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,
# animation_frame=None, animation_group=None,
# category_orders=None, labels=None, orientation=None,
# color_discrete_sequence=None, color_discrete_map=None,
# line_dash_sequence=None, line_dash_map=None,
# log_x=False, log_y=False,
# range_x=None, range_y=None,
# line_shape=None, render_mode='auto', title=None, 
# template=None, width=None, height=None)
# 显示中国的人均寿命
fig = px.line(df, x="year", y="lifeExp", title='中国人均寿命')
fig.show()
 
# 以不同颜色显示亚洲各国的人均寿命
df = px.data.gapminder().query("continent == 'Asia'")
fig = px.line(df, x="year", y="lifeExp", color="country", 
       hover_name="country")
fig.show()
 
# line_group="country" 达到按国家去重的目的
df = px.data.gapminder().query("continent != 'Asia'") # remove Asia for visibility
fig = px.line(df, x="year", y="lifeExp", color="continent",
       line_group="country", hover_name="country")
fig.show()
 
# bar图
df = px.data.gapminder().query("country == 'China'")
fig = px.bar(df, x='year', y='lifeExp')
fig.show()
 
df = px.data.gapminder().query("continent == 'Asia'")
fig = px.bar(df, x='year', y='lifeExp',color="country" )
fig.show()
 
df = px.data.gapminder().query("country == 'China'")
fig = px.bar(df, x='year', y='pop',
       hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
       labels={'pop':'population of China'}, height=400)
fig.show()
 
fig = px.bar(df, x='year', y='pop',
       hover_data=['lifeExp', 'gdpPercap'], color='pop',
       labels={'pop':'population of China'}, height=400)
fig.show()
 
df = px.data.medals_long()
# #     nation  medal count
# # 0 South Korea  gold   24
# # 1    China  gold   10
# # 2    Canada  gold   9
# # 3 South Korea silver   13
# # 4    China silver   15
# # 5    Canada silver   12
# # 6 South Korea bronze   11
# # 7    China bronze   8
# # 8    Canada bronze   12
fig = px.bar(df, x="nation", y="count", color="medal", 
       title="Long-Form Input")
fig.show()
 
# 气泡图
df = px.data.gapminder()
# X轴以对数形式展现
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp",
         size="pop",
         color="continent",hover_name="country", 
         log_x=True, size_max=60)
fig.show()
 
# X轴以标准形式展现
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp",
         size="pop",
         color="continent",hover_name="country", 
         log_x=False, size_max=60)
fig.show()
 
# 饼状图
px.data.gapminder().query("year == 2007").groupby('continent').count()
#      country year lifeExp pop gdpPercap iso_alpha iso_num
# continent
# Africa     52  52    52  52     52     52    52
# Americas    25  25    25  25     25     25    25
# Asia      33  33    33  33     33     33    33
# Europe     30  30    30  30     30     30    30
# Oceania     2   2    2  2     2     2    2
df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")
fig = px.pie(df, values='pop', names='country',
       title='Population of European continent')
fig.show()
 
df.loc[df['pop'] < 10000000, 'country'] = 'Other countries'
fig = px.pie(df, values='pop', names='country', 
       title='Population of European continent',
       hover_name='country',labels='country')
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
 
df.loc[df['pop'] < 10000000, 'country'] = 'Other countries'
fig = px.pie(df, values='pop', names='country', 
       title='Population of European continent',
       hover_name='country',labels='country', 
       color_discrete_sequence=px.colors.sequential.Blues)
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
 
# 二维面积图
df = px.data.gapminder()
fig = px.area(df, x="year", y="pop", color="continent", 
       line_group="country")
fig.show()
 
fig = px.area(df, x="year", y="pop", color="continent", 
       line_group="country",
       color_discrete_sequence=px.colors.sequential.Blues)
fig.show()
 
df = px.data.gapminder().query("year == 2007")
fig = px.bar(df, x="pop", y="continent", orientation='h',
       hover_name='country',
       text='country',color='continent')
fig.show()
 
# 甘特图
import pandas as pd
df = pd.DataFrame([
  dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', 
     Completion_pct=50, Resource="Alex"),
  dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15',
     Completion_pct=25, Resource="Alex"),
  dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', 
     Completion_pct=75, Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", 
         color="Completion_pct")
fig.update_yaxes(autorange="reversed")
fig.show()
 
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", 
         color="Resource")
fig.update_yaxes(autorange="reversed")
fig.show()
 
# 玫瑰环图
df = px.data.tips()
#   total_bill  tip   sex smoker  day  time size
# 0     16.99 1.01 Female   No  Sun Dinner   2
# 1     10.34 1.66  Male   No  Sun Dinner   3
# 2     21.01 3.50  Male   No  Sun Dinner   3
# 3     23.68 3.31  Male   No  Sun Dinner   2
# 4     24.59 3.61 Female   No  Sun Dinner   4
fig = px.sunburst(df, path=['day', 'time', 'sex'], 
         values='total_bill')
fig.show()
 
import numpy as np
df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(df, path=['continent', 'country'], 
         values='pop',
         color='lifeExp', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu',
         color_continuous_midpoint=np.average(df['lifeExp'], 
                            weights=df['pop']))
fig.show()
 
df = px.data.gapminder().query("year == 2007")
fig = px.sunburst(df, path=['continent', 'country'], 
         values='pop',
         color='pop', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu')
fig.show()
 
# treemap图
import numpy as np
df = px.data.gapminder().query("year == 2007")
df["world"] = "world" # in order to have a single root node
fig = px.treemap(df, path=['world', 'continent', 'country'], 
         values='pop',
         color='lifeExp', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu',
         color_continuous_midpoint=np.average(df['lifeExp'], 
                            weights=df['pop']))
fig.show()
 
fig = px.treemap(df, path=['world', 'continent', 'country'], values='pop',
         color='pop', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu',
         color_continuous_midpoint=np.average(df['lifeExp'], 
                            weights=df['pop']))
fig.show()
 
fig = px.treemap(df, path=['world', 'continent', 'country'], values='pop',
         color='lifeExp', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu')
fig.show()
 
fig = px.treemap(df, path=[ 'continent', 'country'], values='pop',
         color='lifeExp', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu')
fig.show()
 
fig = px.treemap(df, path=[ 'country'], values='pop',
         color='lifeExp', hover_data=['iso_alpha'],
         color_continuous_scale='RdBu')
fig.show()
 
# 桑基图
tips = px.data.tips()
fig = px.parallel_categories(tips, color="size",
               color_continuous_scale=px.colors.sequential.Inferno)
fig.show()

到此这篇关于关于Python可视化Dash工具之plotly基本图形示例详解的文章就介绍到这了,更多相关Python plotly基本图形内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    ASP编程JSP编程PHP编程.NET编程python编程