Table of Contents
Matplotlib is a popular Python library used for data visualization. It allows users to create various types of charts and graphs, including bar charts. The width of bars in a bar chart can be adjusted to make the chart more visually appealing and to better represent the data. This can be achieved by using the “width” parameter in the plt.bar() function in Matplotlib. By changing the value of this parameter, users can control the width of the bars in the chart. This allows for more customization and flexibility in creating charts that effectively convey the information being presented. Adjusting the width of bars in Matplotlib is a simple and effective way to enhance the overall appearance and accuracy of bar charts.
Adjust Width of Bars in Matplotlib
You can use the width argument to adjust the width of bars in a bar plot created by Matplotlib:
import matplotlib.pyplotas plt
plt.bar(x=df.category, height=df.amount, width=0.8)
The default value for width is 0.8 but you can increase this value to make the bars wider or decrease this value to make the bars more narrow.
The following example shows how to use this syntax in practice.
Example: Adjust Width of Bars in Matplotlib
Suppose we have the following pandas DataFrame that contains information about the total sales of various products at some grocery store:
import pandas as pd#create DataFrame
df = pd.DataFrame({'item': ['Apples', 'Oranges', 'Kiwis', 'Bananas', 'Limes'],
'sales': [18, 22, 19, 14, 24]})
#view DataFrame
print(df)
item sales
0 Apples 18
1 Oranges 22
2 Kiwis 19
3 Bananas 14
4 Limes 24
We can use the following code to create a bar chart to visualize the number of sales of each item:
import matplotlib.pyplotas plt#create bar chart
plt.bar(x=df.item, height=df.sales)
By default, Matplotlib uses a width of 0.8.
However, we can use the width argument to specify a different value:
import matplotlib.pyplotas plt#create bar chart with narrow bars
plt.bar(x=df.item, height=df.sales, width=0.4)
Notice that the bars are much more narrow.
Also note that if you use a value of 1 for width, the bars will touch each other:
import matplotlib.pyplotas plt#create bar chart with width of 1
plt.bar(x=df.item, height=df.sales, width=1, edgecolor='black')
Feel free to adjust the value for the width argument to make the bars in the plot as wide or narrow as you’d like.
The following tutorials explain how to perform other common tasks in Matplotlib:
Cite this article
stats writer (2024). How can the width of bars in Matplotlib be adjusted?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-width-of-bars-in-matplotlib-be-adjusted/
stats writer. "How can the width of bars in Matplotlib be adjusted?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-width-of-bars-in-matplotlib-be-adjusted/.
stats writer. "How can the width of bars in Matplotlib be adjusted?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-width-of-bars-in-matplotlib-be-adjusted/.
stats writer (2024) 'How can the width of bars in Matplotlib be adjusted?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-width-of-bars-in-matplotlib-be-adjusted/.
[1] stats writer, "How can the width of bars in Matplotlib be adjusted?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the width of bars in Matplotlib be adjusted?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
