Get startedGet started for free

Building Python-based visualizations

1. Building Python-based visualizations

In this video, we will use Python to build visualizations, some not native to Power BI. Specifically, a line plot, pair plot, and joint plot using the Seaborn package. The dataset is from another online retailer with customer demographics, spending habits, and a variable named “Response”, indicating a customer interacted with a campaign. Let’s start with a line plot. Add a new Python visual. We will be using “Age”, “Income”, and “Kidhome”. Resize the python visualization to use the whole page; it will be helpful later. Import matplotlib dot pyplot - as plt - and seaborn - as sns. The seaborn dot lineplot function requires three parameters - data, which will be our dataset from the selected columns; x, which will be Age; and y, which will be Income. We set these parameters with the column names as strings. An optional parameter, “hue”, allows us to color lines based on the distinct values of the specified column. We will use Kidhome. Add plt dot show() to render the plot in Power BI. Execute the script. Customers without kids - the light line - make more income. Those with kids - the dark lines - increase their income as they get older. The shaded area is a confidence interval for the line - we will not dive into this here as it is out of scope for this course. We can use other Power BI visualizations - plots and/or filters - to interact with our python visualization. However, we can’t interact with it directly as we can with a native Power BI visualization. Let’s build a pair plot on a new page called “pairplot”. These are useful for evaluating the pairwise relationships across multiple variables. We can add a handful of variables in the dataset. Be careful not to add too many because the pair plot will become too chaotic to read. We will add all the variables starting with Mnt. These are the amount spent across product categories. Also, the income and Kidhome variables. Import matplotlib dot pyplot -as plt -and seaborn -as sns. Creating a pair plot is as simple as passing a DataFrame to the sns dot pairplot() function. Our DataFrame is called “dataset” - pass this as the argument. Use plt dot show() to render the visualization to Power BI. Click execute. There are lots of blobs of points or weak relationships between the pairs of variables. However, there are a couple that exhibit a moderate to strong relationship. One example is “Income” vs “MntTotal” - as the income increases, so does the total amount spent. We can zoom into this relationship with a joint plot. A joint plot will replicate the pairwise scatter plot between Income and MntTotal and add the individual distributions. Create a new Python visual on a new page; adding the two variables Income and MntTotal. A joint plot can be created using sns dot jointplot(). It takes three parameters - data, which will be our dataset; x, which will be Income; and y, which will be MntTotal. Use plt dot show(), then run the script. We see the same scatterplot with a positive relationship. We also see the distribution for the Income variable above, which is normal if not slightly bimodal. We also see the distribution for MntTotal on the right. It is clearly skewed. Most customers have income on the lower end of the range, with fewer having an income on the higher end. These are simple examples of custom plots you can build using Python. As you gain more experience, if you don’t have it already, you can explore even more complex chart options and design elements. Now it’s your turn!

2. Let's practice!