This article describes how to use matplotlib to draw a bar chart in python. the bar chart effects include basic bar charts, stacked column charts, parallel column charts, bar charts, and various style settings of bar charts, for more information, see the following. Matplotlib is not described here.
I have also shared with you the line chart and pie chart effects achieved by using matplotlib in python before. if you are interested, you can click to view the results. let's take a look at how python uses matplotlib to draw a bar chart, the details are as follows:
1. basic bar chart
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data)plt.show()
For example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar([0.3, 1.7, 4, 6, 7], data, width=0.6, bottom=[10, 0, 5, 0, 5])plt.show()
2. set the bar style
(1) color
You can use the facecolor (or fc) keyword parameter to set the color of the bar, for example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, fc='g')plt.show()
You can use the color keyword to set multiple colors at a time. for example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, color='rgb') # or `color=['r', 'g', 'b']`plt.show()
(2) stroke
The related keyword parameters are:
Edgecolor or ec
Linestyle or ls
Linewidth or lw
For example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, ec='r', ls='--', lw=2)plt.show()
(3) filling
The hatch keyword can be used to set the fill style. the optional values are:/, \, |,-, +, x, o, O ,.,*. For example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, ec='k', lw=1, hatch='o')plt.show()
3. set tick label
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']plt.bar(range(len(data)), data, tick_label=labels)plt.show()
4. stacked column chart
You can use the bottom parameter to draw a stacked column chart. For example:
import numpy as npimport matplotlib.pyplot as pltsize = 5x = np.arange(size)a = np.random.random(size)b = np.random.random(size)plt.bar(x, a, label='a')plt.bar(x, b, bottom=a, label='b')plt.legend()plt.show()
5. parallel column chart
Similar to the stacked column chart, a parallel column chart is used to draw multiple groups of bars. you only need to control the position and size of each group of bars. For example:
import numpy as npimport matplotlib.pyplot as pltsize = 5x = np.arange(size)a = np.random.random(size)b = np.random.random(size)c = np.random.random(size)total_width, n = 0.8, 3width = total_width / nx = x - (total_width - width) / 2plt.bar(x, a, width=width, label='a')plt.bar(x + width, b, width=width, label='b')plt.bar(x + 2 * width, c, width=width, label='c')plt.legend()plt.show()
6. bar chart
Use the barh method to draw a bar chart. For example:
import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.barh(range(len(data)), data)plt.show()
The signature of the plt. barh method is:
barh(bottom, width, height=0.8, left=None, **kwargs)
The method is similar to that of plt. bar. Therefore, the stacked bar chart and parallel bar chart are similar to the preceding method.
7. positive and negative Bar chart
import numpy as npimport matplotlib.pyplot as plta = np.array([5, 20, 15, 25, 10])b = np.array([10, 15, 20, 15, 5])plt.barh(range(len(a)), a)plt.barh(range(len(b)), -b)plt.show()
For more articles about how to use matplotlib to draw a bar chart in python, refer to PHP!