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)
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排名数据,该数据具有诸如球员姓名,排名得分,球员排名和排名日期之类的信息。 在流式处理中,您还可以将数据框查看为表格。 经过一些基本的格式化后,我的最终数据帧如下所示。
图表将显示什么? (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
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.
图表动画 (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)以以下方式显示数据框对象。
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排名的整体得分发生了变化,其他球员也在随后的几年中进入。 他们当前的排名显示为条形标签。 条形图经过排序,以便排名较高的玩家始终出现在顶部。
结论 (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,一经查实,立即删除!