Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Customizing Bar Charts | Bar Charts
Visualization in Python with matplotlib
course content

Contenido del Curso

Visualization in Python with matplotlib

Visualization in Python with matplotlib

1. Basics: Line Charts
2. Bar Charts
3. Scatter Plots

Customizing Bar Charts

Nicely done! You've built many interesting bar charts. Now it's time to add some information to make the plots informative.

Like, in the previous chapters, we can use the .set() function applied to the Axes object to set the following parameters: xlabel, ylabel for labels on the axes, xlim, ylim to limit values on the axes (this parameters must have list/tuple as a value), title for chart title. Also, to improve the readability we can add values above (or right/left) to each bar. To do it, we should save in separate variable each call of .bar() (or .barh()) function, and then call the .bar_label() applied to Axes object, passing saved .bar() function call as the first argument, and padding as the second (optional, the distance between bar and text). For example,

123456789101112131415161718
# Import library import matplotlib.pyplot as plt # Create data for chart subjects = ['Math', 'Literature', 'History', 'Physics', 'Arts'] grades = [95, 76, 83, 92, 68] # Create Axes and Figure objects fig, ax = plt.subplots() # Initialize bar chart bar1 = ax.bar(subjects, grades) # Add labels above bars ax.bar_label(bar1, padding = 0) # Display the plot plt.show()
copy

¿Todo estuvo claro?

Sección 2. Capítulo 9
We're sorry to hear that something went wrong. What happened?
some-alt