Plotting Bar Charts
Besides plotting line charts, you can also plot bar charts using matplotlib. Bar charts are useful for comparing data. For example, you want to be able to compare the grades of a student over a number of semesters.
Using the same dataset that you used in the previous section, you can plot a bar chart using the bar()
function as follows:
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
plt.bar(
[1,2,3,4,5,6,7,8,9,10],
[2,4.5,1,2,3.5,2,1,2,3,2],
label = "Jim",
color = "m", # m for magenta
align = "center"
)
plt.title("Results")
plt.xlabel("Semester")
plt.ylabel("Grade")
plt.legend()
plt.grid(True, color="y")
Figure 4.7 shows the bar chart plotted using the preceding code snippet.
Adding Another Bar to the Chart
Just like adding an additional line chart to...