Pie charts are one of the most powerful visualizations when presenting them. With a few tricks you can make them look professional with a free tool like Matplotlib.
In the end of this tutorial you will know how to make pie charts and customize it even further.
Basic Pie Chart
First you need to make a basic Pie chart with matplotlib.
Actually Data Visualization is an important skill to understand and present data.
This is a key skill in Data Science. If you like to learn more then check my free Expert Data Science Blueprint course with the following resources.
15 video lessons – covers the Data Science Workflow and concepts, demonstrates everything on real data, introduce projects and shows a solution (YouTube video).
30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
15 projects – structured with the Data Science Workflow and a solution explained in the end of video lessons (GitHub).
You see that data is not totally scattered all over, but is not fully correlated either. This means, that there is come weak correlation of the data and it is not fully independent of each other.
#4 Box Plot
One way to understand data better is by a box plot. It might need a bit of understanding of simple statistics.
Let’s first take a look at it.
data.plot.box()
The box plot shows the following.
To understand what outliers, min, median, max, and so forth means, I would suggest you read this simple statistic guide.
An area plot can show you the data in a great way to see how the values follow each other in a visual easy way to get an understanding of values, correlation, and missing data.
data.plot.area(figsize=(12,4), subplots=True)
#6 Bar plots
Bar plots can be useful, but often when the data is more limited.
Here you see a bar plot of the first 15 rows of data.
data.iloc[:15].plot.bar()
#7 Histograms for single column
Histograms will show you what data is most common. It shows the frequencies of data divided into bins. By default there are 10 bins of data.
It is an amazing tool to get a fast view of the number of occurrences of each data range.
Here first for an isolated station.
data['station_paris'].plot.hist()
#8 Histograms for multiple columns
Then for all three stations, where you see it with transparency (alpha).
data.plot.hist(alpha=.5)
#9 Pie
Pie charts are very powerful, when you want to show a division of data.
How many percentage belong to each category.
Here you see the mean value of each station.
data.mean().plot.pie()
#10 Scatter Matrix Plot
This is a great tool for showing data for combined in all possible ways. This will show you correlations and how data is distributed.
You need to import an additional library, but it gives you fast understanding of data.
from pandas.plotting import scatter_matrix
scatter_matrix(data, alpha=0.2, figsize=(6, 6))
#11 Secondary y-axis
Finally, sometimes you want two plots on the same chart. The problem can be, that the two plots have very different ranges. hence, you would like to have two different y-axes, with different ranges.
This will enable you to have plots on the same chart with different ranges.
Want to learn more about Data Science to become a successful Data Scientist?
Then check my free Expert Data Science Blueprint course with the following resources.
15 video lessons – covers the Data Science Workflow and concepts, demonstrates everything on real data, introduce projects and shows a solution (YouTube video).
30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
15 projects – structured with the Data Science Workflow and a solution explained in the end of video lessons (GitHub).
In this tutorial you will learn why One Liners is something beginners focus on and senior developers don’t waste their time on. But You will learn some useful things you can do in one line of Python code.
Why are One-Liners not (always) good?
Why are one-liners are bad?
If I could say it one word: Readability.
Most beginners, I was the same, are focus on solving the problem, and later, often solving it in some impressive way.
Why do senior developers not do that? Well, they spend hours debugging code – code should be easy to understand and maintain.
Yes, senior developers know that code that can written in one line, is often difficult to get useful stack traces from, when they fail. They know it is better to focus on breaking the code up in multiple lines. This makes the stack trace easier to get the error. Also, it enhances the readability of the code. Which makes it easier to understand and therefore to maintain.
Now let’s dive into 15 useful things you still can do in one line of Python code without compromising readability.
#1 Swap Two Variables
This one might not be fully appreciated if you have not been coding in another language.
Take a moment and think about the following problem.
You have two drinks.
Your job is to switch the content of the glass. That is, the blue drink should be in the glass of the red drink, and vice verse.
Obviously, to do this, you need a third glass or something similar.
This is the same problem when you need to swap (switch) the the “content” of two variables.
As you need to do this often as a programmer, Python has made this easy for you.
a = 10
b = 20
print(a, b)
# Swap the two variables
a, b = b, a
print(a, b)
This will swap the content. First print will output 10 20 and second 20 10.
#2 Reverse a List
First of all, Python lists are amazing. Again, if you worked with other programming languages, you will fall in love with how easy it is to work with Python lists.
Anyhow, sometimes you need to get the content from a list in reversed order.
This can be done easily as follows.
l = [1, 2, 3, 4, 5]
print(l[::-1])
This will output 5, 4, 3, 2, 1.
Check out the last bonus trick how this can be useful to know for a job interview.
#3 Calculate the mode of a list
First of all, what is the mode of a list?
Good question my friend. It is the most common element in a list. This is often useful to know.
Let’s see how this can be done.
l = [1,3,2,5,2,2,5,4]
mode = max(set(l), key=l.count)
print(mode)
This will output 2, as it is the most common element.
How to understand the code?
I am happy you asked.
The set(l) gives a set of the list, which is all the unique element in the list. Here it give {1, 3, 2, 5, 4}.
Then max(set(l), key=l.count) gives the maximum value of each value in set with the count of it. Hence, you get the value with the highest count.
#4 Strip lines for start and end spaces and remove new lines
When you read lines from a text file, it can have leading and ending spaces, as well as lines with no content.
This is an example of lines read from a text file.
lines = [' THE ADVENTURE OF THE NOBLE BACHELOR\n',
'\n',
' The Lord St. Simon marriage, and its curious termination, have long\n']
To remove (or strip) for leading and ending spaces, you can do the following.
lines = [line.strip() for line in lines]
print(lines)
Which will result in.
['THE ADVENTURE OF THE NOBLE BACHELOR',
'',
'The Lord St. Simon marriage, and its curious termination, have long']
Notice it also remove new lines.
If you want to remove empty lines. This can be done as follows.
lines = [line for line in lines if len(line) > 0]
print(lines)
This will result in.
['THE ADVENTURE OF THE NOBLE BACHELOR',
'The Lord St. Simon marriage, and its curious termination, have long']
#5 Multiple variable assignment
Sometimes code can become really long, if you have a lot of variable you need to assign to specific values.
This can be done in one line.
a, b, c = 4.4, 'Awesome', 7
print(a)
print(b)
print(c)
This will output.
4.4
Awesome
7
Notice the different types of the variables.
#6 Convert a string into a number
I actually love this one. Why? Because in Python it just a built-in function to convert a string to number.
It will print the values and the type of the variables, int and float, respectively.
#7 Type casting a list of items
This is a great use of List Comprehension.
Say, you have a list of strings with integers. It can happen you read a text file, and each line has integers. Then you need to convert them to integers to use the values.
This can be done as follows.
l = ['12', '23', '34']
items = [int(i) for i in l]
Wow. Did you see that? We just used what we learned in last step and combine it with List Comprehensions.
This is quite handy to know how to take the square root of a number without using math libraries.
print(16**.5)
Well, the 16**.5 syntax (the double **) puts the value (here 16) to the power of the exponent (here .5). This lifts 16 to the power of a half (.5). This is the same as taking the square root.
Hence, it will print 4.
#9 How to get the cube root of a number
This one is almost the same. But remember, you get a bonus one in the end, so you will get 15 one-liners that useful, if you feel cheated by this one.
The cube root means, given a number x, find a number y such that y*y*y equals x.
How do you do that?
I actually expect that many do not know that. I didn’t before I studied high-level math in college.
Here we go.
print(27**(1/3))
Ah, you see. You lift to the power of one third. It will print 3, as 3*3*3 is 27.
#10 Get the absolute value of a number
Again a great built-in function to know.
I often need the absolute values of a number. This can be achieved by using abs().