Creating a Pie Chart Based on CSV Data

Python 3: Automating Your Job Tasks Superhero Level: Data Visualization with Bokeh and Python 3
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.01
List Price:  €128.82
You save:  €36.80
£78.38
List Price:  £109.74
You save:  £31.35
CA$136.52
List Price:  CA$191.13
You save:  CA$54.61
A$150.48
List Price:  A$210.68
You save:  A$60.19
S$134.87
List Price:  S$188.83
You save:  S$53.95
HK$780.66
List Price:  HK$1,092.95
You save:  HK$312.29
CHF 91.34
List Price:  CHF 127.88
You save:  CHF 36.54
NOK kr1,052.31
List Price:  NOK kr1,473.28
You save:  NOK kr420.96
DKK kr686.58
List Price:  DKK kr961.24
You save:  DKK kr274.66
NZ$162.83
List Price:  NZ$227.97
You save:  NZ$65.13
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,719.82
List Price:  ৳16,408.23
You save:  ৳4,688.40
₹8,312.02
List Price:  ₹11,637.16
You save:  ₹3,325.14
RM469.75
List Price:  RM657.67
You save:  RM187.92
₦147,331.26
List Price:  ₦206,269.66
You save:  ₦58,938.40
₨27,796.36
List Price:  ₨38,916.02
You save:  ₨11,119.65
฿3,662.34
List Price:  ฿5,127.43
You save:  ฿1,465.08
₺3,217.98
List Price:  ₺4,505.30
You save:  ₺1,287.32
B$517.06
List Price:  B$723.91
You save:  B$206.84
R1,837.02
List Price:  R2,571.91
You save:  R734.88
Лв180.27
List Price:  Лв252.38
You save:  Лв72.11
₩136,066.92
List Price:  ₩190,499.14
You save:  ₩54,432.21
₪368.12
List Price:  ₪515.38
You save:  ₪147.26
₱5,814.46
List Price:  ₱8,140.48
You save:  ₱2,326.02
¥15,687.99
List Price:  ¥21,963.82
You save:  ¥6,275.82
MX$1,668.59
List Price:  MX$2,336.09
You save:  MX$667.50
QR364.34
List Price:  QR510.10
You save:  QR145.75
P1,357.56
List Price:  P1,900.64
You save:  P543.07
KSh13,298.67
List Price:  KSh18,618.67
You save:  KSh5,320
E£4,713.99
List Price:  E£6,599.78
You save:  E£1,885.78
ብር5,741.12
List Price:  ብር8,037.80
You save:  ብር2,296.67
Kz84,932.10
List Price:  Kz118,908.34
You save:  Kz33,976.24
CLP$90,081.08
List Price:  CLP$126,117.11
You save:  CLP$36,036.03
CN¥724.33
List Price:  CN¥1,014.10
You save:  CN¥289.76
RD$5,884.09
List Price:  RD$8,237.97
You save:  RD$2,353.87
DA13,459.27
List Price:  DA18,843.53
You save:  DA5,384.25
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.15
List Price:  Q1,086.64
You save:  Q310.49
GY$20,904.26
List Price:  GY$29,266.81
You save:  GY$8,362.54
ISK kr13,793.62
List Price:  ISK kr19,311.62
You save:  ISK kr5,518
DH996.63
List Price:  DH1,395.33
You save:  DH398.69
L1,771.78
List Price:  L2,480.56
You save:  L708.78
ден5,664.24
List Price:  ден7,930.17
You save:  ден2,265.92
MOP$804.05
List Price:  MOP$1,125.70
You save:  MOP$321.65
N$1,836.30
List Price:  N$2,570.90
You save:  N$734.59
C$3,677.56
List Price:  C$5,148.73
You save:  C$1,471.17
रु13,287.88
List Price:  रु18,603.56
You save:  रु5,315.68
S/373.53
List Price:  S/522.95
You save:  S/149.42
K388.30
List Price:  K543.64
You save:  K155.33
SAR375.02
List Price:  SAR525.05
You save:  SAR150.02
ZK2,666.83
List Price:  ZK3,733.66
You save:  ZK1,066.83
L457.94
List Price:  L641.14
You save:  L183.19
Kč2,276.37
List Price:  Kč3,187.01
You save:  Kč910.64
Ft35,391.42
List Price:  Ft49,549.41
You save:  Ft14,157.98
SEK kr1,062.40
List Price:  SEK kr1,487.41
You save:  SEK kr425
ARS$89,066.09
List Price:  ARS$124,696.09
You save:  ARS$35,630
Bs690.41
List Price:  Bs966.60
You save:  Bs276.19
COP$385,967.67
List Price:  COP$540,370.17
You save:  COP$154,402.50
₡51,227.77
List Price:  ₡71,720.93
You save:  ₡20,493.15
L2,469.21
List Price:  L3,457
You save:  L987.78
₲751,546.38
List Price:  ₲1,052,195.01
You save:  ₲300,648.62
$U3,849.38
List Price:  $U5,389.29
You save:  $U1,539.90
zł391.29
List Price:  zł547.82
You save:  zł156.53
Already have an account? Log In

Transcript

Welcome back, it's time to learn how to generate yet another type of plot the pie plot, this time using the data stored inside the CSV file, instead of an Excel spreadsheet as in the previous lecture, again, I'm going to create and use a new Jupiter notebook inside the same folder as before. So let me create this new notebook new Python three, I'm going to name it pi, enter the code inside this notebook is going to read and loads data from the countries dot CSV file located in the same folder containing the top 10 countries by population at the time of recording this video. Basically, as you can see right here, on the top row, we have the name of each country and the associated population. Of course, this set of data can be much larger, but I prefer to keep things simple and Clean for now, so you can grasp the necessary concepts better.

Now, let me paste in the code, and let's discuss it line by line to show how to achieve our goal. So this is the code right here, we want to create a pie chart that will illustrate the top 10 countries by population. Additionally, we want our plots to have a nice legend with an individual color for each country. And as a bonus, we want to be able to hover our mouse over each wedge of the resulting pie chart and see the country name and population. So this is the code let's run it and see the end result along with the legend and the hover option as well. So Ctrl plus enter.

This is the pie chart. This is the legend on the right side. And whenever we hover our mouse over each of these wedges, we see the name of the country and the population as I already said, Okay, that's pretty cool. Now let's get our hands dirty with some code. First of all, As we do each and every time, we have to import the necessary tools and libraries, of course, we are going to need several tools from the bouquet library, as well as the pandas module that will help us load the data from the CSV file. Moreover, we will need to import the PI from within the math module.

So we can configure our pie chart. Don't worry, math is a built in Python module, so you don't need to install it separately. I'm not going to explain in detail any operations or lines of code that we've already covered in the previous videos, just to save some time and get to the things that set this application apart from the others. So in short, we are configuring the output file as being pi dot html. Then we are loading the data inside the CSV file as a panda's data frame. And then we are simply referencing the data stored in each column using two distinct variables, country and population.

Next, it's time to catch up collate the angle of each wedge in the pie chart, based on the population value for each country, without entering into too much detail on the math behind this, the formula to obtain the angle of each wedge is x times two times pi divided by the sum of X. So for instance, to find out the angle for China's wedge in this chart, we need to multiply the value representing China's population with two and with pi, and then divide the result by the sum of all the population values in the table. And that's exactly what this line of code does right here. It creates a panda's series object containing the angles for all the wages of the pie, calculated using the formula I mentioned. Two things to notice here. First, we use the sum method to quickly add up all the population values in that column of the data frame.

Secondly, I hope you remember the order of evaluation when it comes To mathematical expressions, multiplication and division have equal priorities. So they are evaluated from left to right. This means that we are performing the division operation, and then we are multiplying the result by two and pi. Next, as we did in the previous video, we want each of our pie wedges to have a different color. So we are going to need a palette. This time I chose another palette called category 20 C, which is basically yet another list of hex color codes.

In fact, let me prove this to you in the Python interpreter. So let me import the bullet first from bulky dot bullets. Import category 20 C. Now let's see it category 20 C. And now as you can see in our application code, we are passing the length of the data frame. So let me show you land of data with passing the land Have the data frame which is 10. So if we go back to the Python interpreter, we're passing the value of 10 to the category 20 c Polet, meaning that we want a different color for each of the 10 countries inside the data frame. So if we use category 20 C of 10.

In the interpreter, we get a list of 10 different color codes, which is exactly what we want. Now let's get back to the code. Next, we are creating the figure object just as we did in previous lectures using the figure function from within the bulky library. As always, we are pressing several arguments to customize our plot. So we are configuring the plot height to 400, the plot title, we're also hiding the toolbar as we did with our bar plot. And the awesome feature here is the enabling of the hover option using the tools argument.

So we have tools equals Over. And following this argument, we insert yet another argument called tooltips, which basically references the country name and population value for each row in the data frame creates a mapping between the two, and finally displays this mapping for each wedge of the pie whenever we hover our mouse over it, as you've just seen, next, we are using the wedge method to define the way we build each wedge of the pie chart. First, we're passing the x and y values, which in our case are zero and one. To better understand the meaning of these two values. Let me set this attribute right here to true. This will show the axes on our plot.

Let's hit control plus enter and see the plot now. So as you can notice right here, the center of our pie chart has two coordinates, zero for the x axis and one for the y axis. Now you understand the meaning behind the first two arguments instead. In the parentheses of the wedge method, let me set this back to false. Next, we have the radius size right here in between the parentheses of the wedge method, which is set to 0.4. In this case, you can increase the radius of your pie chart if you need to.

But please keep in mind that you should also edit the block height value up here in between the parentheses of the figure function, and also the x and y values. Otherwise, your pie chart will exceed the dimensions of the plot. So let me modify the radius to show you what I mean by that. Let's set it to, let's say 0.6. At this point, if we run this code, as I said, the pie chart has gone beyond the limits of your figure. To fix this, let's also increase the plot height value to let's say, 800.

Then let's also add the plot with arguments. So plot underscore with Let's set it to 800 as well. And finally, let's set the center of our pie chart two different coordinates, let's say zero point 25 and 1.5 for y. Now, if we run our code once again, we have an invalid syntax. That's because I forgot to add the comma. So Ctrl plus enter once again.

Let's see what else. Oh, I also misspelled with Okay, I hope this fixes everything. Yeah, Ctrl, Enter. And now as you can see, we have a much larger pie chart available, which doesn't exceed the figure itself. But for now, let's undo these changes. So I'm going to set the height back to 400.

Then I'm going to delete the plot width argument. And also I'm going to stick with x equals zero and y equals one and radius equals 0.4 as we previously had. Now, if we run our code once again, we have the initial the original pie chart in place. Okay, now let me close some of these plots. And now let's take care of the rest of the code. So inside the parentheses of the wedge method, we also have the come some function that defines the starting and ending points for our plotting values.

For instance, our pie chart starts with China as its first wedge. So let me run this once again. So China is the first wedge, and if we look carefully, it starts at exactly zero degrees. As you can see on the chart, notice this white line between the blue edge representing China and the light green wedge associated with Mexico. So back to our code, the line color argument right here, in between the parentheses of the wedge match It defines the line color, this being the line, the color of the line between the widgets and surrounding these little squares as well in the legend. If you don't trust me, let's replace white, right here with red, for example.

And let's run our code once again. Now you can see that all the white lines have been replaced with red lines. Okay, let's undo this setting as well. Okay, next, we are also specifying the field color by referencing the category 20 c palette that we've discussed earlier in this video. Then we have a label for the legend called country. And finally, we are setting the data source for each of the wedges as being our panda's data frame.

Pretty cool. Last but not least, we're also inserting some additional options such as hiding the axis labels, hiding the axes themselves, and also hiding the gridlines in order To have a nice and clean pie chart. As always, we are ending our application by using the show function and passing our figure object as an argument to this function, so we can have the pie plot displayed to the screen. That's it with pie charts, feel free to visit the links I have inserted in the comments of this application. To learn more about other options and features that you can add to your own by charts, see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.