Tạo thanh
Với Pyplot, bạn có thể sử dụng hàm bar[]
để vẽ biểu đồ thanh
Ví dụ
Vẽ 4 vạch
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. thanh[x,y]
plt. hiển thị []
Kết quả
Hàm bar[]
nhận các đối số mô tả bố cục của các thanh
Các danh mục và giá trị của chúng được biểu thị bằng đối số thứ nhất và thứ hai dưới dạng mảng
Thanh ngang
Nếu bạn muốn các thanh được hiển thị theo chiều ngang thay vì theo chiều dọc, hãy sử dụng chức năng barh[]
Ví dụ
Vẽ 4 thanh ngang
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. barh[x, y]
plt. hiển thị []
Kết quả
Màu thanh
bar[]
và barh[]
lấy đối số từ khóa
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]0 để đặt màu cho các thanh
Ví dụ
Vẽ 4 thanh màu đỏ
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. thanh[x, y, color = "đỏ"]
plt. hiển thị []
Kết quả
Tên màu
Bạn có thể sử dụng bất kỳ tên nào trong số 140 tên màu được hỗ trợ
Ví dụ
Vẽ 4 thanh "hot pink"
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. bar[x, y, color = "hotpink"]
plt. hiển thị []
Kết quả
màu lục giác
Hoặc bạn có thể sử dụng các giá trị màu thập lục phân
Ví dụ
Vẽ 4 vạch bằng màu xanh rất đẹp
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. bar[x, y, color = "#4CAF50"]
plt. hiển thị []
Kết quả
chiều rộng thanh
bar[]
lấy đối số từ khóa
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]2 để đặt chiều rộng của các thanh
Ví dụ
Vẽ 4 thanh rất mỏng
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. thanh[x, y, chiều rộng = 0. 1]
plt. hiển thị []
Kết quả
Giá trị chiều rộng mặc định là 0. 8
Ghi chú. Đối với thanh ngang, sử dụng
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]3 thay vì
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]2
Chiều cao thanh
barh[]
lấy đối số từ khóa
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]3 để đặt chiều cao của các thanh
Ví dụ
Vẽ 4 thanh rất mỏng
nhập matplotlib. pyplot dưới dạng plt
nhập numpy dưới dạng np
x = np. mảng[["A", "B", "C", "D"]]
y = np. mảng[[3, 8, 1, 10]]
plt. thanh[x, y, chiều cao = 0. 1]
plt. hiển thị []
Kết quả
Giá trị chiều cao mặc định là 0. 8
Tuy nhiên, hàm
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]7 lấy một danh sách các vị trí và giá trị, các nhãn cho x sau đó được cung cấp bởi
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]8
Trong 1]
import matplotlib.pyplot as plt %matplotlib inline plt.style.use['ggplot'] x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green'] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]
thanh lỗi
Chúng ta có thể mở rộng phần trên với các thanh lỗi như sau
Trong 2]
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]
Biểu đồ thanh ngang
Chúng tôi có thể hiển thị cùng một biểu đồ theo chiều ngang bằng cách sử dụng
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.bar[x_pos, energy, color='green', yerr=variance] plt.xlabel["Energy Source"] plt.ylabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.xticks[x_pos, x] plt.show[]9
Trong 3]
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.barh[x_pos, energy, color='green', xerr=variance] plt.ylabel["Energy Source"] plt.xlabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.yticks[x_pos, x] plt.show[]
Biểu đồ thanh có nhiều chữ X
Để bao gồm nhiều giá trị X trên cùng một biểu đồ, chúng ta có thể giảm chiều rộng của các thanh và sau đó đặt các chỉ số cách xa trục y một chiều rộng của thanh
Trong [4]
import numpy as np N = 5 men_means = [20, 35, 30, 35, 27] women_means = [25, 32, 34, 20, 25] ind = np.arange[N] width = 0.35 plt.bar[ind, men_means, width, label='Men'] plt.bar[ind + width, women_means, width, label='Women'] plt.ylabel['Scores'] plt.title['Scores by group and gender'] plt.xticks[ind + width / 2, ['G1', 'G2', 'G3', 'G4', 'G5']] plt.legend[loc='best'] plt.show[]
Biểu đồ thanh xếp chồng lên nhau
Với các biểu đồ thanh xếp chồng lên nhau, chúng tôi cần cung cấp tham số
x = ['Nuclear', 'Hydro', 'Gas', 'Oil', 'Coal', 'Biofuel'] energy = [5, 6, 15, 22, 24, 8] variance = [1, 2, 7, 4, 2, 3] x_pos = [i for i, _ in enumerate[x]] plt.barh[x_pos, energy, color='green', xerr=variance] plt.ylabel["Energy Source"] plt.xlabel["Energy Output [GJ]"] plt.title["Energy output from various fuel sources"] plt.yticks[x_pos, x] plt.show[]0, điều này thông báo cho matplotlib vị trí bắt đầu của thanh, vì vậy chúng tôi sẽ cộng các giá trị bên dưới
Trong [5]
countries = ['USA', 'GB', 'China', 'Russia', 'Germany'] bronzes = np.array[[38, 17, 26, 19, 15]] silvers = np.array[[37, 23, 18, 18, 10]] golds = np.array[[46, 27, 26, 19, 17]] ind = [x for x, _ in enumerate[countries]] plt.bar[ind, golds, width=0.8, label='golds', color='gold', bottom=silvers+bronzes] plt.bar[ind, silvers, width=0.8, label='silvers', color='silver', bottom=bronzes] plt.bar[ind, bronzes, width=0.8, label='bronzes', color='#CD853F'] plt.xticks[ind, countries] plt.ylabel["Medals"] plt.xlabel["Countries"] plt.legend[loc="upper right"] plt.title["2012 Olympics Top Scorers"] plt.show[]
Nếu chúng tôi muốn xem cùng một biểu đồ thanh nhưng theo tỷ lệ của tổng số huy chương mà quốc gia đó giành được, chúng tôi có thể làm như sau