Matplotlib PyPlot – Set Line Width for Bar Plot

To set line width for bars in a Bar Plot in Matplotlib, call matplotlib.pyplot.bar() function, and pass required line width as float value for linewidth parameter of bar() function.

The definition of matplotlib.pyplot.bar() function with linewidth parameter is

</>
Copy
bar(x, height, linewidth=None)

Of course, there are other named parameters, but for simplicity, only linewidth parameter is given along with required x and height. The default value of linewidth parameter is None.

where

ParameterDescription
linewidthThe width of bar edge(s) in the bar plot. The parameter takes a float value. If 0, don’t draw edges.

Example

In the following program, we will draw a bar plot with bars having 3.0 as line width. Also, we will set fill parameter with False, so that we will see only the edges of bar faces.

example.py

</>
Copy
import matplotlib.pyplot as plt

#data
x = [1, 2, 3, 4, 5]
h = [10, 8, 12, 4, 7]

#bar plot
plt.bar(x, height=h, fill=False, linewidth=3.0)

plt.show()

Output

Matplotlib PyPlot - Set Line Width for Bar Plot
Line Width for Bar Plot set to 3.0

Conclusion

In this Matplotlib Tutorial, we learned how to set line width for bars in bar plot using Matplotlib PyPlot API.