Mirrored

I see her in your eyes. “Mirrored” is published by Hannah Ramezanzadeh.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Seaborn Visualization for Beginners

Wondering how to create data visualizations using Python but not sure how? Maybe you’re not sure what library can generate the type of chart you want.

Part 1: Optimizing SQL Temp Tables Using This Trick?

Part 2: Using Python To Transform Data From Multiple CSV Files

To start, I import several libraries into my Jupyter Notebook, set the Pandas library to display the maximum number of columns, and change the percent format into the common (0.00%) format. I prefer to see all the columns rather than have Pandas condense it. I also preset the Seaborn settings for each figure size, the color palette and more.

At the same time, I start the timer to time how long it takes to load my four CSV files. I always use this method because sometimes the CSV files are very large and it’s important to know how long it takes to import.

Now that everything has been loaded into the notebook, I check the columns and datatypes using:

I notice that my date column is an “object”; I will need to change that to a datetime n64. I also notice that the size column will need to be changed to a “category” datatype and set the size order so the charts will respond accordingly.

Now that I’ve correctly changed the data type into category and set the order for the size column, I’ll work on changing the date column into a date datatype.

This date conversion method is unusual but effective. I joined the date and time columns into the date column, then converted it into a datetime format.

I then chose to change all dates within each month to the first day of the month. This makes it easier to place the data into the charts for this entry level analysis. If you wanted, you could create a new data frame that has the unique dates and the other is grouped by month.

For each new data frame, I’m specifically picking features that I find important.

For this data frame, I’m counting the number of transactions for each month.

I then create another data frame that counts the number of pizzas sold for each month.

quantitysold.head(2) Output

Finally, I create a temporary data frame called temp1 that counts the number of pizzas, orders by the pizza size and the month. This data frame is tricky to create but when you break down the code, you can see how it works.

As a note, when you use the backslash “\”, this allows you to continue the python code to the next line break without generating an error.

Lastly, I merge the data frames created above into one data frame to generate my charts from. This makes creating the charts faster and the script runs faster as the data frame only contains what is needed.

df2.head(2)

Next, I’ll reorder the columns and rename one of them.

With Seaborn, there are many ways you can create charts or modifying multiple charts at the same time. For my examples, I’ve kept them simple and clear. As you become more comfortable, you will find your own method. Like all things with Python, you can chain things together to make mass updates or modifications.

To start, all seaborn charts will start with its abbreviation, which is typically “sns”. Next, I type the chart I want and for my first chart, I selected a line plot. Next, I specify my data frame and the two columns I want for the X and Y axis.

As a best practice tip, I always write out the X and Y axis labels.

Line Chart

For the multiple bar chart, I will use the same template as above except this is called a bar plot. I also use a built-in function called “hue” which allows the chart to plot each unique value separately.

As a best practice tip, I always write out the X and Y axis labels and for this chart, I also included a legend that aligns with each bar.

For these new data frames, I will take the category name and count how many orders are within each of the categories.

SM Data Frame Output

Next, I create a new data frame that counts the number of orders by category by date.

dc.head(2)

The code below shows how to create a pie chart that as the category name labeled on the outside, the percentage on the inside and exploded to help your eyes see the slight difference between them all.

Pie Chart Output

Finally, my last chart displays the pizza sales by month and category. To do this, I use the new data frame that I created above and set my hue to ‘category’.

Multiple Line Chart Output

This completes my basic introduction for Seaborn and how to create multiple types of charts using specific data frames. Not all charts will require a new data frame but often I’ve found it helpful. Remember, everyone has a preference and every situation is different. Don’t get discouraged because any process is better than no process.

Don’t forget to follow me and if you’re interested in learning more about bar charts and how to improve them, check it out — Improve Your Bar Chart Visuals With These Five Tips | by Da Data Guy | Medium

Add a comment

Related posts:

The Storm

It was a bright winter morning. The sun decided to shine a little brighter today, so much so that I did not even snooze my alarm after 7 am . I woke up to streaks of bright yellow light peeping…

Work From at Home

If you are reading this, then you want to know how to work from at home. I first need to tell you something very important. This is not going to be the typical fluff and “how to make money by…

Musings and self reflection during the lockdown

Life is a funny condition. You pop into this world out of no choice of your own. You don’t get to choose your family or the circumstances around your birth but there you are, full of promise…