python 图表_使用Streamlit-Python将动画图表添加到仪表板

python 图表

介绍 (Introduction)

I have been thinking of trying out Streamlit for a while. So last weekend, I spent some time tinkering with it. If you have never heard of this tool before, it provides a very friendly way to create custom interactive Data web-apps for your Data Science and Machine Learning projects using Python. You can interact with the user to get inputs and display dynamic charts, tables, maps etc. Their official gallery might be a good place to start. It will give you a glimpse of what you can achieve with Streamlit. You would also find a long list of tweets from the Streamlit community going creative with the tool in their own unique ways. Do check them out for some inspiration. Streamlit can be downloaded using the pip install command (pip install streamlit).

我一直在考虑尝试Streamlit一段时间。 因此,上周末,我花了一些时间进行修改。 如果您以前从未听说过此工具,那么它提供了一种非常友好的方法,可以使用Python为您的数据科学和机器学习项目创建自定义的交互式数据Web应用程序。 您可以与用户进行交互以获取输入并显示动态图表,表格,地图等。他们的官方画廊可能是一个不错的起点。 它将使您了解Streamlit可以实现的目标。 您还将从Streamlit社区中找到一长串推文,以自己的独特方式来使用该工具。 请检查一下它们是否有启发。 可以使用pip install命令( pip install streamlit )下载Streamlit。

In this article, I have shared a sample animated bar chart built using Streamlit. Although, some of the information to achieve this is available in bits and pieces, I couldn’t find much information on animated charts in one place. I hope this will help somebody who is looking to add an animated chart in their Streamlit Dashboard.

在本文中,我分享了使用Streamlit构建的示例动画条形图。 尽管实现这一目标的一些信息是零碎的,但我无法在一处找到有关动画图表的大量信息。 我希望这会对希望在其Streamlit仪表板中添加动画图表的人有所帮助。

数据集 (The Dataset)

Image for post
Photo by Carlo Bazzo on Unsplash
Carlo Bazzo在Unsplash上拍摄的照片

Let’s talk about the data a little bit. Tennis being my favorite sport, I decided to do build something around it (and also because I really really really miss Wimbledon 💔). Anyway, after scouting the internet for a while, I stumbled across something that I thought I could use. Roger Federer, Rafael Nadal, Novak Djokovic & Andy Murray were fondly referred as the ‘Big Four’ until 2017. Between them, they have dominated the sport from 2004 to 2017.

让我们来谈谈数据。 网球是我最喜欢的运动,我决定围绕它做一些事情(也是因为我真的非常想念温布尔登网球公开赛)。 无论如何,在搜寻互联网一段时间后,我偶然发现了一些我认为可以使用的东西。 罗杰·费德勒(Roger Federer),拉斐尔·纳达尔(Rafael Nadal),诺瓦克·德约科维奇(Novak Djokovic)和安迪·穆雷(Andy Murray)一直被誉为“ 四大 ”,直到2017年。他们在2004年至2017年期间一直主导这项运动。

The data set has weekly ATP ranking data which has the information like the player name, ranking points, player ranking, and date of ranking. In streamlit, you can also view your dataframe as a table. After some basic formatting my final dataframe looks like below.

该数据集具有每周ATP排名数据,该数据具有诸如球员姓名,排名得分,球员排名和排名日期之类的信息。 在流式处理中,您还可以将数据框查看为表格。 经过一些基本的格式化后,我的最终数据帧如下所示。

Image for post
Arindom BhattacharjeeArindom Bhattacharjee

图表将显示什么? (What the chart will show?)

I wanted to build an animated bar chart that shows how the Big Four ranked against each other for this period of almost two decades. For the animation, I kept replacing the weekly data in the in chart after a certain time delay inside a for loop . This gave it an animated look and feel.

我想制作一个动画的条形图,以显示在过去的二十年中四大巨头之间的排名。 对于动画,在for循环内经过一定时间延迟后,我一直替换图表中的每周数据。 这赋予了它生动的外观。

For visualization purposes, initially I started with the good old Matplotlib library but I ran into some issues and decided to go forward with Altair, which is another visualization library you can use with python. In my experience, I found it to have a better look and feel than Matplotlib to use in my data app. Streamlit supports both of these libraries and a few others as well.

出于可视化的目的,最初我是从旧的M atplotlib库开始的,但是遇到了一些问题,并决定继续使用Altair ,后者是另一个可以与python一起使用的可视化库。 根据我的经验,我发现它在数据应用程序中使用的效果比Matplotlib更好。 Streamlit支持这两个库以及其他一些库。

用户界面 (User Interface)

For the UI, I decided to give users the ability to select the range of year themselves and run the animation for the time period of their choice. After selecting the time range, once they press the button ‘Cue Chart’ the animations would begin. I have used the ‘Range Slider’ & ‘Button’ features to perform this.

对于用户界面,我决定让用户自己选择年份范围并在所选时间段内运行动画。 选择时间范围后,一旦他们按下“ 提示图表 ”按钮,动画就会开始。 我已经使用了“ 范围滑块 ”和“ 按钮 ”功能来执行此操作。

Range Slider: The purpose of the range slider is to select a range of numbers. It can be created with this single line of code. The function returns a tuple with values selected by the user.

范围滑块范围滑块的目的是选择一个数字范围。 可以使用这一行代码创建它。 该函数返回具有用户选择的值的元组。

x = st.slider('Select the year range',1996, 2017, (1996, 2017))# 'Select the year range' -> Text to display
# 1996 -> The lower bound
# 2017 -> The higher bound
# (1996, 2017) -> Default selected range
Image for post
Arindom BhattacharjeeArindom Bhattacharjee制作的滑块图像

Button: I added a button because I didn’t want the animation to start on its own after the user selects the time range. The button can be created as shown below. I run the animation code inside this ‘if’ statement. The st.button() function returns a boolean true when user performs a click event.

按钮 :我添加了一个按钮,因为我不希望动画在用户选择时间范围后自行开始。 可以如下所示创建按钮。 我在“ if ”语句中运行动画代码。 当用户执行click事件时,st.button()函数将返回布尔值true。

if(st.button('Cue Chart')):
#run the animation. Value is 'True' when user clicks on it.
Image for post
Arindom BhattacharjeeArindom Bhattacharjee

图表动画 (Chart Animation)

I used the Altair python library for creating the bar chart. I created a function (plot_bar_animated_altair) that takes two inputs - the weekly data for the Big Four players and week name. It then plots a bar chart. My final formatted dataframe looks like below which will be used to plot the charts. The streamlit function st.dataframe(df) displays a dataframe object in the following fashion.

我使用Altair python库创建条形图。 我创建了一个函数( plot_bar_animated_altair ),该函数需要两个输入-四大玩家的每周数据和周名称。 然后绘制条形图。 我最终格式化的数据框如下所示,将用于绘制图表。 简化函数st.dataframe(df)以以下方式显示数据对象。

Image for post
Arindom BhattacharjeeArindom Bhattacharjee
import streamlit as st
import pandas as pd
import time
import altair as alt
from altair import Chart, X, Y, Axis, SortField, OpacityValue#---------------------------------------------------------------#
# Creating an empty chart in the beginning when the page loads
#---------------------------------------------------------------#
bars = alt.Chart(data).mark_bar().encode(
x=X('1:Q',axis=Axis(title='ATP Ranking Points'),
y=Y('0:Q',axis=Axis(title='The Big Four'))
).properties(
width=650,
height=400
)# This global variable 'bar_plot' will be used later onbar_plot = st.altair_chart(bars)
def plot_bar_animated_altair(df,week):
bars = alt.Chart(df, title="Ranking as of week :"+week).encode(
x=X('ranking_points:Q',axis=Axis(title='ATP Ranking
Points'),
y=Y('full_name:N',axis=Axis(title='The Big Four'), sort='-x'),
color=alt.Color('full_name:N'),
.properties(
width=650,
height=400
)if st.button('Cue Chart'):
for week in week_list: # weekly_df -> this dataframe (sample shown above) contains
# data for a particular week which is passed to
# the 'plot_bar_animated_altair' function. # week -> Current week title, eg:- 2016-06-10

bars = plot_bar_animated_altair(weekly_df,week)
time.sleep(0.01)

bar_plot.altair_chart(bars)

st.balloons() # Displays some celebratory balloons for glamour!

演示版 (Demo)

In the demonstration below, I have selected the entire range from 1996 to 2017 and in the beginning you will notice only one bar here (for Roger Federer) because the other three had not turned professional yet. As the weeks pass, you can see the overall ATP Ranking points change and the other players enter in the subsequent years. Their current ranking is displayed as a label against the bars. The bars are sorted so that the higher ranked player always appears on the top.

在下面的演示中,我选择了1996年至2017年的整个范围,一开始您会注意到这里只有一个标杆(对Roger Federer而言),因为其他三个标杆还没有变成专业人士。 随着星期的过去,您可以看到ATP排名的整体得分发生了变化,其他球员也在随后的几年中进入。 他们当前的排名显示为条形标签。 条形图经过排序,以便排名较高的玩家始终出现在顶部。

Arindom BhattacharjeeArindom Bhattacharjee

结论 (Conclusion)

In conclusion, by the end of this side project I was quite impressed with the simplicity of Streamlit. To achieve this using any other tool would have definitely taken me a lot more time. I hope this article helps save you some time if you are looking to create an animated chart.

最后,在本项目结束时, Streamlit的简单性给我留下了深刻的印象。 要使用其他任何工具来实现此目标,肯定会花费我更多时间。 如果希望创建动画图表,希望本文能为您节省一些时间。

Given the growing support that it is receiving lately, I am sure they will add a ton of other useful features. Thank you for reading and wish you a great week ahead!

鉴于最近它得到越来越多的支持,我相信他们会添加很多其他有用的功能。 感谢您的阅读,并祝您度过愉快的一周!

翻译自: https://towardsdatascience.com/add-animated-charts-to-your-dashboards-with-streamlit-python-f41863f1ef7c

python 图表

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389245.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python--day26--复习

转载于:https://www.cnblogs.com/xudj/p/9953293.html

SiameseRPN++分析

SiamRPN论文来源论文背景什么是目标跟踪什么是孪生网络结构Siamese的局限解决的问题论文分析创新点一:空间感知策略创新点二:ResNet-50深层网络创新点三:多层特征融合创新点四:深层互相关代码分析整体代码简述(1&#…

Lockdown Wheelie项目

“It’s Strava for wheelies,” my lockdown project, combining hyper-local exercise with data analytics to track and guide improvement. Practising wheelies is a great way to stay positive; after all, it’s looking up, moving forward.我的锁定项目“将Strava运…

api地理编码_通过地理编码API使您的数据更有意义

api地理编码Motivation动机 In my second semester of my Master’s degree, I was working on a dataset which had all the records of the road accident in Victoria, Australia (2013-19). I was very curious to know, which national highways are the most dangerous …

SiamBAN论文学习

SiameseBAN论文来源论文背景主要贡献论文分析网络框架创新点一:Box Adaptive Head创新点二:Ground-truth创新点三:Anchor Free论文流程训练部分:跟踪部分论文翻译Abstract1. Introduction2. Related Works2.1. Siamese Network Ba…

实现klib_使用klib加速数据清理和预处理

实现klibTL;DRThe klib package provides a number of very easily applicable functions with sensible default values that can be used on virtually any DataFrame to assess data quality, gain insight, perform cleaning operations and visualizations which results …

MMDetection修改代码无效

最近在打比赛,使用MMDetection框架,但是无论是Yolo修改类别还是更改head,代码运行后发现运行的是修改之前的代码。。。也就是说修改代码无效。。。 问题解决办法: MMDetection在首次运行后会把一部分运行核心放在anaconda的环境…

docker etcd

etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库,用于配置共享和服务发现 etcd内部采用raft协议作为一致性算法,etcd基于Go语言实现。 etcd作为服务发现系统,有以下的特点&…

SpringBoot简要

2019独角兽企业重金招聘Python工程师标准>>> 简化Spring应用开发的一个框架;      整个Spring技术栈的一个大整合;      J2EE开发的一站式解决方案;      自动配置:针对很多Spring应用程序常见的应用功能&…

简明易懂的c#入门指南_统计假设检验的简明指南

简明易懂的c#入门指南介绍 (Introduction) One of the main applications of frequentist statistics is the comparison of sample means and variances between one or more groups, known as statistical hypothesis testing. A statistic is a summarized/compressed proba…

Torch.distributed.elastic 关于 pytorch 不稳定

错误日志: Epoch: [229] Total time: 0:17:21 Test: [ 0/49] eta: 0:05:00 loss: 1.7994 (1.7994) acc1: 78.0822 (78.0822) acc5: 95.2055 (95.2055) time: 6.1368 data: 5.9411 max mem: 10624 WARNING:torch.distributed.elastic.agent.server.api:Rec…

0x22 迭代加深

poj2248 真是个新套路。还有套路剪枝...大到小和判重 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<bitset> using namespace std;int n,D,x[110];bool…

云原生全球最大峰会之一KubeCon首登中国 Kubernetes将如何再演进?

雷锋网消息&#xff0c;11月14日&#xff0c;由CNCF发起的云原生领域全球最大的峰会之一KubeConCloudNativeCon首次登陆中国&#xff0c;中国已经成为云原生领域一股强大力量&#xff0c;并且还在不断成长。 毫无疑问&#xff0c;Kubernetes已经成为容器编排事实标准&#xff…

分布分析和分组分析_如何通过群组分析对用户进行分组并获得可行的见解

分布分析和分组分析数据分析 (DATA ANALYSIS) Being a regular at a restaurant is great.乙 eing定期在餐厅是伟大的。 When I started university, my dad told me I should find a restaurant I really liked and eat there every month with some friends. Becoming a reg…

python 工具箱_Python交易工具箱:通过指标子图增强图表

python 工具箱交易工具箱 (trading-toolbox) After a several months-long hiatus, I can finally resume posting to the Trading Toolbox Series. We started this series by learning how to plot indicators (specifically: moving averages) on the top of a price chart.…

PDA端的数据库一般采用的是sqlce数据库

PDA端的数据库一般采用的是sqlce数据库,这样与PC端的sql2000中的数据同步就变成了一个问题,如在PDA端处理,PDA端的内存,CPU等都是一个制约因素,其次他们的一个连接稳定及其间的数据传输也是一个难点.本例中通过在PC端的转化后再复制到PDA上面,这样,上面所有的问题都得到了一个有…

bzoj 1016 [JSOI2008]最小生成树计数——matrix tree(相同权值的边为阶段缩点)(码力)...

题目&#xff1a;https://www.lydsy.com/JudgeOnline/problem.php?id1016 就是缩点&#xff0c;每次相同权值的边构成的联通块求一下matrix tree。注意gauss里的编号应该是从1到...的连续的。 学习了一个TJ。用了vector。自己曾写过一个只能过样例的。都放上来吧。 路径压缩的…

商米

2019独角兽企业重金招聘Python工程师标准>>> 今天看了一下商米的官网&#xff0c;发现他家的东西还真的是不错。有钱了&#xff0c;想去体验一下。 如果我妹妹还有开便利店的话&#xff0c;我会推荐他用这个。小巧便捷&#xff0c;非常方便。 转载于:https://my.osc…

python交互式和文件式_使用Python创建和自动化交互式仪表盘

python交互式和文件式In this tutorial, I will be creating an automated, interactive dashboard of Texas COVID-19 case count by county using python with the help of selenium, pandas, dash, and plotly. I am assuming the reader has some familiarity with python,…

不可不说的Java“锁”事

2019独角兽企业重金招聘Python工程师标准>>> 前言 Java提供了种类丰富的锁&#xff0c;每种锁因其特性的不同&#xff0c;在适当的场景下能够展现出非常高的效率。本文旨在对锁相关源码&#xff08;本文中的源码来自JDK 8&#xff09;、使用场景进行举例&#xff0c…