Often when visualizing data using a bar chart, you’ll have to make a decision about the orientation of your bars. While there are no concrete rules, there are quite a few factors that can go into making this decision. For example, when grouping your data by an ordinal variable, you may want to display those groupings along the x-axis. On the other hand, when grouping your data by a nominal variable, or a variable that has long labels, you may want to display those groupings horizontally to aid in readability. Show
This recipe will show you how to go about creating a horizontal bar chart using Python. Specifically, you’ll be using pandas In our example, you'll be using the publicly available San Francisco bike share trip dataset to identify the top 15 bike stations with the highest average trip durations. You will then visualize these average trip durations using a horizontal bar chart. The steps in this recipe are divided into the following sections: You can find implementations of all of the steps outlined below in this example Mode report. Let’s get started. Data WranglingYou’ll use SQL to wrangle the data you’ll need for our analysis. For this example, you’ll be using the
Once the SQL query has completed running, rename your SQL query to Data AnalysisNow that you have your data wrangled, you’re ready to move over to the Python notebook to prepare your data for visualization. Inside of the Python notebook, start by importing the Python modules that you'll be using throughout the remainder of this recipe:
Mode pipes the results of your SQL queries into a pandas assigned to the variable
As previously mentioned, your goal is to visualize the 15 start stations with the highest average trip duration. You can analyze the dataframe to find these stations using the following method chain on our existing dataframe object:
We now have a new dataframe assigned to the variable 0 that contains the top 15 start stations with the highest average trip durations. Now that we have our dataset aggregated, we are ready to visualize the data.Data VisualizationTo create a horizontal bar chart, we will use pandas 2 to the 3 argument:
Pandas returns the following horizontal bar chart using the default settings: You can use a bit of matplotlib styling functionality to further customize and clean up the appearance of your visualization: Ever thought you could build a real-time dashboard in Python without writing a single line of HTML, CSS, or Javascript? Yes, you can! In this post, you’ll learn:
Can’t wait and want to jump right in? Here's the code repo and the video tutorial. What’s a real-time live dashboard?A real-time live dashboard is a web app used to display Key Performance Indicators (KPIs). If you want to build a dashboard to monitor the stock market, IoT Sensor Data, AI Model Training, or anything else with streaming data, then this tutorial is for you. 1. How to import the required libraries and read input dataHere are the libraries that you’ll need for this dashboard:
Go ahead and import all the required libraries:
You can read your input data in a CSV by using 3. But remember, this data source could be streaming from an API, a JSON or an XML object, or even a CSV that gets updated at regular intervals.Next, add the 3 call within a new function 5 so that it gets properly cached.What's caching? It's simple. Adding the decorator 6 will make the function 5 run once. Then every time you rerun your app, the data will stay memoized! This way you can avoid downloading the dataset again and again. Read more about caching in Streamlit docs.
2. How to do a basic dashboard setupNow let’s set up a basic dashboard. Use 8 with parameters serving the following purpose:
3. How to design a user interfaceA typical dashboard contains the following basic UI design components:
Let’s drill into them in detail. Page title The title is rendered as the <h1> tag. To display the title, use 2. It’ll take the string “Real-Time / Live Data Science Dashboard” and display it in the Page Title.
Top-level filter First, create the filter by using 3. It’ll display a dropdown with a list of options. To generate it, take the unique elements of the 4 column from the dataframe df. The selected item is saved in an object named 5:
Now that your filter UI is ready, use 5 to filter your dataframe df.
KPIs/summary cards Before you can design your KPIs, divide your layout into a 3 column layout by using 7. The three columns are kpi1, kpi2, and kpi3. 8 helps you create a KPI card. Use it to fill one KPI in each of those columns. 8’s label helps you display the KPI title. The value **is the argument that helps you show the actual metric (value) and add-ons like delta to compare the KPI value with the KPI goal.
Interactive charts Split your layout into 2 columns and fill them with charts. Unlike the metric above, use the 0 clause to fill the interactive charts in the respective columns:
Data table Use 1 to display the data frame. Remember, your data frame gets filtered based on the filter option selected at the top:
4. How to refresh the dashboard for real-time or live data feedSince you don’t have a real-time or live data feed yet, you’re going to simulate your existing data frame (unless you already have a live data feed or real-time data flowing in). To simulate it, use a 2 loop from 0 to 200 seconds (as an option, on every iteration you’ll have a second 3/pause):
Inside the loop, use NumPy's 4 to generate a random number between 1 to 5. Use it as a multiplier to randomize the values of age and balance columns that you’ve used for your metrics and charts.5. How to auto-update componentsNow you know how to do a Streamlit web app! To display the live data feed with auto-updating KPIs/Metrics/Charts, put all these components inside a single-element container using 5. Call it 6: 0Put your components inside the 6 by using a 0 clause. This way you’ll replace them in every iteration of the data update. The code below contains the 9 along with the UI components you created above: 1And...here is the full code! 2To run this dashboard on your local computer:
Wrapping upCongratulations! You have learned how to build your own real-time live dashboard with Streamlit. I hope you had fun along the way. If you have any questions, please leave them below in the comments or reach out to me at [email protected] or on Linkedin. Langkah langkah dalam memvisualisasikan data?Langkah-langkah membuat visualisasi data. Tentukan pertanyaan terkait data. ... . Pahami data dan tentukan bentuk visualnya. ... . 3. Identifikasi pesan yang ingin disampaikan. ... . Pilih bentuk visual yang akan digunakan. ... . Kreasikan dengan berbagai warna dan bentuk.. Kenapa sih perlu data visualization?Fungsi Data Visualization
Visualisasi data memberikan informasi yang sangat berguna untuk kepentingan bisnis. Pengambil keputusan di perusahaan akan dapat dengan mudah melihat dan memahami mengenai hasil kerja perusahaan, berdasarkan variabel-variabel yang dimiliki.
Apa yang dimaksud dengan Data Visualization?Visualisasi data (data visualization) merupakan rangkaian proses yang akan dilakukan setiap data analis untuk menampilkan data atau informasi dalam bentuk yang agar mudah dipahami oleh orang awam, seperti grafik, angka dan lain sebagainya. Menggunakan data visual memberikan banyak sekali manfaat.
Apa nama pustaka Python yang digunakan untuk membuat visualisasi data?Matplotlib adalah library Python yang digunakan untuk membuat visualisasi data agar lebih menarik dan mudah dipahami.
|