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
€93.41
List Price:  €130.79
You save:  €37.37
£79.90
List Price:  £111.86
You save:  £31.96
CA$137.16
List Price:  CA$192.03
You save:  CA$54.87
A$153.08
List Price:  A$214.33
You save:  A$61.24
S$135.90
List Price:  S$190.27
You save:  S$54.36
HK$781.66
List Price:  HK$1,094.36
You save:  HK$312.69
CHF 91.23
List Price:  CHF 127.73
You save:  CHF 36.49
NOK kr1,107.76
List Price:  NOK kr1,550.92
You save:  NOK kr443.15
DKK kr696.86
List Price:  DKK kr975.63
You save:  DKK kr278.77
NZ$168.60
List Price:  NZ$236.05
You save:  NZ$67.44
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,972.60
List Price:  ৳15,362.09
You save:  ৳4,389.48
₹8,346.11
List Price:  ₹11,684.89
You save:  ₹3,338.77
RM475.54
List Price:  RM665.78
You save:  RM190.23
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,846.85
List Price:  ₨38,986.71
You save:  ₨11,139.85
฿3,694.58
List Price:  ฿5,172.56
You save:  ฿1,477.98
₺3,236.41
List Price:  ₺4,531.11
You save:  ₺1,294.69
B$519.35
List Price:  B$727.11
You save:  B$207.76
R1,861
List Price:  R2,605.48
You save:  R744.47
Лв182.73
List Price:  Лв255.83
You save:  Лв73.10
₩137,535.16
List Price:  ₩192,554.72
You save:  ₩55,019.56
₪374.71
List Price:  ₪524.61
You save:  ₪149.90
₱5,763.02
List Price:  ₱8,068.46
You save:  ₱2,305.43
¥15,525.12
List Price:  ¥21,735.79
You save:  ¥6,210.67
MX$1,695.96
List Price:  MX$2,374.42
You save:  MX$678.45
QR364.20
List Price:  QR509.89
You save:  QR145.69
P1,420.18
List Price:  P1,988.32
You save:  P568.13
KSh13,372.99
List Price:  KSh18,722.73
You save:  KSh5,349.73
E£4,798.26
List Price:  E£6,717.75
You save:  E£1,919.49
ብር5,719.92
List Price:  ብር8,008.12
You save:  ብር2,288.20
Kz83,641.63
List Price:  Kz117,101.63
You save:  Kz33,460
CLP$96,028.39
List Price:  CLP$134,443.59
You save:  CLP$38,415.20
CN¥723.89
List Price:  CN¥1,013.48
You save:  CN¥289.58
RD$5,819.58
List Price:  RD$8,147.64
You save:  RD$2,328.06
DA13,455.35
List Price:  DA18,838.03
You save:  DA5,382.68
FJ$226.17
List Price:  FJ$316.65
You save:  FJ$90.48
Q777.12
List Price:  Q1,088.01
You save:  Q310.88
GY$20,929.88
List Price:  GY$29,302.67
You save:  GY$8,372.79
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.06
List Price:  DH1,415.53
You save:  DH404.46
L1,768.33
List Price:  L2,475.73
You save:  L707.40
ден5,750.59
List Price:  ден8,051.06
You save:  ден2,300.46
MOP$805.69
List Price:  MOP$1,128
You save:  MOP$322.31
N$1,863.42
List Price:  N$2,608.86
You save:  N$745.44
C$3,672.13
List Price:  C$5,141.13
You save:  C$1,469
रु13,425.24
List Price:  रु18,795.88
You save:  रु5,370.63
S/376.35
List Price:  S/526.91
You save:  S/150.55
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,677.10
List Price:  ZK3,748.04
You save:  ZK1,070.94
L464.84
List Price:  L650.79
You save:  L185.95
Kč2,347.91
List Price:  Kč3,287.17
You save:  Kč939.26
Ft36,370.18
List Price:  Ft50,919.71
You save:  Ft14,549.52
SEK kr1,092.76
List Price:  SEK kr1,529.91
You save:  SEK kr437.15
ARS$87,614.14
List Price:  ARS$122,663.30
You save:  ARS$35,049.16
Bs690.85
List Price:  Bs967.22
You save:  Bs276.36
COP$390,329.27
List Price:  COP$546,476.60
You save:  COP$156,147.32
₡51,017.42
List Price:  ₡71,426.43
You save:  ₡20,409.01
L2,476.75
List Price:  L3,467.55
You save:  L990.79
₲749,236.02
List Price:  ₲1,048,960.40
You save:  ₲299,724.38
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł404.64
List Price:  zł566.52
You save:  zł161.87
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.