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 automatically pipes the results of your SQL queries into a pandas dataframe 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 Data VisualizationTo create a horizontal bar chart, we will use pandas
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:
Running this block of code returns the following visualization: Apa perbedaan Series dengan DataFrame?Series diibaratkan sebagai array satu dimensi sama halnya dengan numpy array, hanya bedanya mempunyai index dan kita dapat mengontrol index dari setiap elemen tersebut. Sedangkan data frame merupakan array dua dimensi dengan baris dan kolom.
Library Pandas buat apa?Mengenal Library Pandas dalam Python
Salah satu tools yang mungkin paling sering digunakan oleh data scientist adalah Pandas. Dalam bahasa pemrograman Python, Pandas adalah alat yang sangat berguna sebagai library yang mengatur tata letak data sehingga mudah dicari secara intuitif.
NumPy untuk apa?NumPy merupakan salah satu library Python yang berfungsi untuk proses komputasi numerik. NumPy memiliki kemampuan untuk membuat objek N-dimensi array. Array merupakan sekumpulan variabel yang memiliki tipe data yang sama.
Apa itu Pandas DataFrame?Pandas DataFrame adalah struktur data 2 Dimensi. Data distrukturisasi seperti tabel yang berisi baris dan kolom, sehingga mudah untuk melakukan queri atau mengakses data tersebut. Baris merepresentasikan record dan kolom merepresentasikan field.
|