Là một phần mở rộng cho các câu trả lời trước đó, bạn có thể sử dụng plt.hbar
:
import matplotlib.pyplot as plt
import numpy as np
import string
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
y = np.array([0, 0, 1, 0, 0, 0, 1, 1, 2, 2, 0, 2, 0, 2])
labels = np.array(list(string.uppercase))
plt.barh(y, [1]*len(x), left=x, color = 'red', edgecolor = 'red', align='center', height=1)
plt.ylim(max(y)+0.5, min(y)-0.5)
plt.yticks(np.arange(y.max()+1), labels)
plt.show()
Hoặc, bạn có thể thử somethings như thế này:
import matplotlib.pyplot as plt
import numpy as np
data = [[1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 2, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 3, 0, 3]]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axes.get_yaxis().set_visible(False)
ax.set_aspect(1)
def avg(a, b):
return (a + b)/2.0
for y, row in enumerate(data):
for x, col in enumerate(row):
x1 = [x, x+1]
y1 = np.array([y, y])
y2 = y1+1
if col == 1:
plt.fill_between(x1, y1, y2=y2, color='red')
plt.text(avg(x1[0], x1[1]), avg(y1[0], y2[0]), "A",
horizontalalignment='center',
verticalalignment='center')
if col == 2:
plt.fill_between(x1, y1, y2=y2, color='orange')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "B",
horizontalalignment='center',
verticalalignment='center')
if col == 3:
plt.fill_between(x1, y1, y2=y2, color='yellow')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "C",
horizontalalignment='center',
verticalalignment='center')
plt.ylim(3, 0)
plt.show()
Nếu bạn muốn tất cả các vị trí phải được trên cùng một dòng, chỉ cần thực hiện một vài thay đổi như hình dưới đây:
import matplotlib.pyplot as plt
import numpy as np
data = [[1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 2, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 3, 0, 3]]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axes.get_yaxis().set_visible(False)
ax.set_aspect(1)
def avg(a, b):
return (a + b)/2.0
for y, row in enumerate(data):
for x, col in enumerate(row):
x1 = [x, x+1]
y1 = [0, 0]
y2 = [1, 1]
if col == 1:
plt.fill_between(x1, y1, y2=y2, color='red')
plt.text(avg(x1[0], x1[1]), avg(y1[0], y2[0]), "A",
horizontalalignment='center',
verticalalignment='center')
if col == 2:
plt.fill_between(x1, y1, y2=y2, color='orange')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "B",
horizontalalignment='center',
verticalalignment='center')
if col == 3:
plt.fill_between(x1, y1, y2=y2, color='yellow')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "C",
horizontalalignment='center',
verticalalignment='center')
plt.ylim(1, 0)
plt.show()
Các tùy chọn thứ hai và thứ ba là mã nhiều hơn, nhưng họ mang lại kết quả tốt hơn nhiều.
+1 cho đóng đinh yêu cầu đầu tiên với âm mưu cuối cùng của bạn. – Yann
có tên không (ví dụ: ô nhịp xếp chồng, biểu đồ đường khu vực, v.v.) cho loại biểu đồ cuối cùng? – arturomp