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
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
Parameter | Description |
---|---|
linewidth | The 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
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
Conclusion
In this Matplotlib Tutorial, we learned how to set line width for bars in bar plot using Matplotlib PyPlot API.