Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
The bar chart
The bar chart has rich forms of expression, common types include single group bar chart, multiple group bar chart, accumulation bar chart and symmetrical bar chart.
Single group bar chart
Each representation of a bar chart can be drawn as a vertical or horizontal bar chart, taking the two ways of drawing a single group of bars as an example.
Vertical bar graph
import matplotlib.pyplot as plt
data = [10..20..5..15.]
plt.bar(range(len(data)), data)
plt.show()
Copy the code
The Tips: plt.plot() function accepts two parameters, including the x-coordinate of each bar and the height of each bar. With optional arguments width, pyplot.bar() provides a way to control the width of bars in a bar graph:
import matplotlib.pyplot as plt
data = [10..20..5..15.]
plt.bar(range(len(data)), data, width=0.5)
plt.show()
Copy the code
Horizontal bar chart
If you prefer the horizontal bar appearance, you can use the plt.barh() function, which is basically the same in usage as plt.bar(), but to modify the width of the bar(or what should be called height in horizontal bar graphs), you need to use height:
import matplotlib.pyplot as plt
data = [10..20..5..15.]
plt.barh(range(len(data)), data, height=0.5)
plt.show()
Copy the code
Multiple groups of bars
When we need to compare quarterly sales from year to year and so on, we may need multiple sets of bars.
import numpy as np
import matplotlib.pyplot as plt
data = [[10..20..30..20.], [40..25..53..18.], [6..22..52..19.]]
x = np.arange(4)
plt.bar(x + 0.00, data[0], color = 'b', width = 0.25)
plt.bar(x + 0.25, data[1], color = 'g', width = 0.25)
plt.bar(x + 0.50, data[2], color = 'r', width = 0.25)
plt.show()
Copy the code
Stacking bar chart
A stack bar chart can be drawn by using optional arguments in the plt.bar() function.
import matplotlib.pyplot as plt
y_1 = [3..25..45..22.]
y_2 = [6..25..50..25.]
x = range(4)
plt.bar(x, y_1, color = 'b')
plt.bar(x, y_2, color = 'r', bottom = y_1)
plt.show()
Copy the code
Tips: The optional bottom argument to the plt.bar() function allows you to specify a starting value for the bar graph. You can stack more bars with deferred rendering in conjunction with a for loop:
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[5..30..45..22.], [5..25..50..20.], [1..2..1..1.]])
x = np.arange(data.shape[1])
for i in range(data.shape[0]):
plt.bar(x, data[i], bottom = np.sum(data[:i], axis = 0))
plt.show()
Copy the code
Symmetric bar graph
A simple and useful technique is to draw two bar charts symmetrically. For example, if you want to plot the number of men and women in different age groups:
import numpy as np
import matplotlib.pyplot as plt
w_pop = np.array([5..30..45..22.])
m_pop = np.array( [5..25..50..20.])
x = np.arange(4)
plt.barh(x, w_pop)
plt.barh(x, -m_pop)
plt.show()
Copy the code
The bar graph of the female population is drawn as usual. However, the bar graph of the male population stretches to the left, not the right. The negative values of the data can be used to quickly draw symmetrical bar graphs.