Create Multiple Bar Charts in Python using Matplotlib and Pandas

In this Python visualization tutorial you’ll learn how to create and save as a file multiple bar charts in Python using Matplotlib and Pandas. We’ll easily read in a .csv file to a Pandas dataframe and then let Matplotlib perform the visualization. As a bonus you’ll also learn how to save the plot as a file.

The key to making two plots work is the creation of two axes that will hold the respective bar chart subplots.

# define the figure container and the two plot axes
fig = plt.figure(figsize=(20,5))

# add subplots to the figure (build a 1x2 grid and place chart in the first or second section)
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

Understanding the subplot nomenclature is essential. Adding axes to the figure as part of a subplot arrangement is simple with the fig.add_subplot() call. In this arrangement the first digit is the number of rows, the second represents the number of columns, and the third is the index of the subplot (where we want to place our visualization).

Of course you need to watch the video to see how all of the code comes together.

Also, keep this Matplotlib style sheet reference handy for changing up the style on your visual.

NBA Blocks Assists

As always, If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

See the following links for additional background:

https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_subplot

Advertisements

Sum Top and Bottom 10 Products by Sales in Power BI

In this video we will cover how to calculate the aggregate sum of only the Top and Bottom 10 Product Sales using DAX in Power BI. There are always multiple ways to accomplish a task with Power BI and DAX but I will share the technique I used to visualize the Bottom 10 Sales Products when there is a rare single tie among the products. The solution may be a bit over-engineered to my data-set but the aim is to share an approach you can use to tackle similar data issues in your dashboards. It’s well worth the watch!

I won’t give way the whole video but I’ll share the DAX formula to sum the Top top products by Sales Price from my table named ‘Company Sales Data’.

1_SumSalesTop10Products = 
CALCULATE(
          SUM('Company Sales Data'[Sales Price]),
          TOPN(
               10,GROUPBY('Company Sales Data','Company Sales Data'[Product]),
               CALCULATE(sum('Company Sales Data'[Sales Price]))
              )
         )

I have created a variable named 1_SumSalesTop10Products that uses the CALCULATE function to

  • SUM the [Sales Price] variable from the [Company Sales Data] table (see the first argument to the CALCULATE function);
  • But it only sums the [Sales Price] for the TOP 10 highest selling products, because we use the TOP N function to create a temporary table that only returns the products with the 10 highest aggregated Sales Prices;
    • The GROUP BY function is used to aggregate the table rows by product and then the CALCULATE argument sums the Sales Price for the aggregated products;

Don’t let this scare you off, watch the video to get a better understanding, and to learn how I sum the Bottom 10 products by Sales Price.

As always, get out there and do some great things with your data!

 

 

Top and Bottom 10 Products by Sales Using RANKX in Power BI

In this video we’ll venture outside of the default Power BI TOPN functionality used to isolate the top and bottom N values in a visual. Because you’re an astute follower of my blog and YouTube channel, you want to know more than default functionality. The key to pulling off this feat lies with the RANKX function. By using RANKX to provide a ranking to each row in our data set, we can then determine the TOP and BOTTOM 10 values.

Of course watch the video for further breakdown, but they key to using RANKX effectively in Power BI is to use the ALL function as a parameter. In this way the contents of the entire table are considered for appropriate ranking.

The DESC or ASC parameters will enable the calculation of a rank in either descending or ascending sort order.

And finally the DENSE parameter tells RANKX how ties should be handled in the data. For example if you provide DENSE as a parameter, if 10 values are tied with a rank of 1 then the next value will receive a rank of 2.

Power BI Top 10 and Bottom 10 Thumbnail

Here is an example use of RANKX that will return a ranking of Sales Price by Product, that ranks the results in descending order (highest Sales receive the lowest ranks) and ties are in contiguous order. Watch the video to determine how to calculate the BOTTOM 10 ranking and to use RANKX to enable the top or bottom 10 values in a visualization.

ProductRank = RANKX(ALL('Company Sales Data'[Product]), calculate(sum('Company Sales Data'[Sales Price])),,DESC,dense)

As a refresher, check out this popular video to build the calendar table referenced in this video: Power BI Dashboard Tutorial: Year over Year Difference Analysis

If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

Create a Bar Chart in Python using Matplotlib and Pandas

In this Python visualization tutorial you’ll learn how to create and save as a file a stylish bar chart in Python using Matplotlib and Pandas. We’ll easily read in a .csv file to a Pandas dataframe and then let Matplotlib perform the visualization. As a bonus you’ll also learn how to save the plot as a file.

I am using the Anaconda Distribution which is a great one stop shop for all your python needs as it is a free and open source distribution of python. I love this option because it gives you the ability to quickly download multiple python packages for analyses and visualizations. As a bonus it includes Jupyter notebooks (which is a web based environment for writing code).

Packages Used

In case you aren’t familiar with Python packages:

  • Pandas is the killer app so to speak for reading, writing and wrangling data.
  • Matplotlib is the visualization plotting library we will use.
  • Pyplot is collection of functions that enables changes to a figure. Think of a figure as a container that can contain multiple plots know as axes. We can plot our bar chart in the figure plotting area and then enhance the plot with labels, etc.
  • Finally OS is a module that enables the use of operating system functionality. We’ll use this package to point our working directory to where our .csv file is located.

Once you follow the instructions in the video, you’ll produce an image like the following which can be saved to a directory location of your choice.

Keep this Matplotlib style sheet reference handy for changing up your visual.

NBA Blocks

As always, If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

Create a Tableau Waffle Chart Fast and Easy

In this Tableau tutorial I am serving up some delectable waffles in the form of a fast and easy waffle chart. Watch the video to learn the easiest and quickest way to create a waffle chart in Tableau.

If you’re familiar with the Southeast United States then you know that we love The Waffle House down here. As an homage, I made a simple dashboard in the iconic Waffle House signage style.

A waffle chart depends upon a data connection to the data you wish to visualize and a data connection to the waffle chart template. Once you have these two items setup, you simply create a calculated field that marks the fill percentage in your waffle.

Help yourself to some waffles below:

As always, If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

Create an Interactive Stacked Bar Chart in Tableau

In this post we’ll use the new parameter action functionality in Tableau to create an interactive stacked bar chart. When we select a dimension from the viz, we can drill down into a sub-dimension. Parameter actions are new to Tableau as of version 2019.2. In the video we use an NBA data set which you can download in a workbook if you visit my Tableau Public viz.

This neat little trick relies upon the creation of a parameter named [Team Name Parameter] and two dimensions.

  • Dimension #1 is the [Team Name]
  • Dimension #2 is the [Player Name]

There is an implied hierarchical relationship between [Team Name] and [Player Name] as teams are naturally comprised of players.

We will also create a calculated field named [Player Drill] that is defined as such:

IF [Team Name Parameter] = [Team Name] THEN [Player Name]
ELSE [Team Name]
END

When we place the [Player Drill] calculated field on color on the marks card and setup our parameter actions (watch the video for instruction), the user selection of a [Team Name] on the bar chart will feed the [Team Name Parameter]. This causes all of the players on the selected team to be displayed with their respective breakdown of points scored.

Bonus: There is a FIXED LOD lesson in this video as well as a Star Wars reference which I’m sure you’ll have no issue finding. Here’s a hint:

It’s really not complicated so don’t let all the words here confuse you. Just watch the video and do some great things with your data! Interact with the viz below:

As always, If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

This video was definitely inspired by Kevin Flerlage who has a great blog post on the uses of parameter actions in Tableau. Check it out here.

Create a Customizable Heat Map in Power BI

In this video we’ll learn how to create a customizable heat map in Power BI without using the prepackaged downloadable visual. A heat map (or heatmap) is a graphical representation of data where the individual values contained in a matrix are represented as colors. A heat map helps draw your eye to the most and least popular areas within the matrix. The cells contained within the table either contain color-coded categorical data or numerical data, that is based on a color scale.

Matrix

Wrong Matrix

I have some fun in the video with a dashboard that I constructed using a publicly available data set from Microsoft, but in the lesson we’ll create the following:

Heatmap

Make sure to watch the video, download the data set and follow along with the instructions.

If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.