So far, we only do line plot. This kind of plot is best use for something with time progression, such as hours, days, months, or years. However, other type of data might need other form of plot. The kind of graph you choose determines how people view the associated data, so choosing the right graph from the outset is important. This section will describe the various graph types and provide you with basic examples of how to use them.
Pie charts focus on showing parts of a whole. The following example shows how to create a pie chart with many of the special features in place.
We will use ford_escort.csv, that shows used Ford Escorts for sale. There are 23 records of year, mileage and price.
First, we need to get how many used car for each production year. We can count the number of year using .value_counts() as in example.
From that DataFrame, we will make a pie chart by plt.pie().
We set the values to be the column of "Year" and the labels to be the index (containing years). In this example, we also set the colors and explode effect.
The argument autopct='%1.1f%%' shows the percentage inside each pie.In most cases, you also want to give your chart a title. You do this using the title() function.Bar charts make comparing values easy. The following example shows just some of the things you can do with a vertical bar chart.
Let's compare how many hurricanes happening each year from 2005 - 2015 using hurricanes.csv we had before. We will first delete the unnecessary columns then and sum the column using .sum()
To create a basic bar chart, you must provide a series of x coordinates and the values of the bars. The example uses the range() function to create the x coordinates.
To simplify making the list for colors and widths, we use list comprehension.
In addition, we will put emphasis on the first column since it gives the highest value. We do this by changing the width and colors for index 0.
We will be using airtravel.csv again for this example.
1. Make a pie chart showing the percentage of flight in each month of 1958. Do the following configuration:
Label it using the "Month" in csv.
Make all the color blue 'b' but choose red 'r' for July and August.
Make it slightly explode (0.1) for everything. Make July and August explode more (0.3).
Show the percentage value of each slice.
Add shadow.
Put in the appropriate title.
2. Make a bar chart showing the sum of flight in 1958, 1959, and 1960. Do the following configuration:
Label it using each respective year.
Make the color of all bars to be cyan 'c'.
Put the same width of 0.6 for all.
Add labels for y-axis only.
Find a way to print the values on top of each bars.
Find a way so that the y-axis start from 4000 and end with 6000.
Make the ticks on y-axis only shows multiple of 200.