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()
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 2.94
Customizing Bar Charts
Deslize para mostrar o menu
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()
Obrigado pelo seu feedback!