Đôi khi có thể là khó khăn để tìm ra chiều rộng thanh phù hợp. Tôi thường sử dụng np.diff này để tìm đúng kích thước.
import numpy as np
import matplotlib.pyplot as plt
#The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = [5.5,6,7,8.5,8.9]
#Calculate optimal width
width = np.min[np.diff[indices]]/3
fig = plt.figure[]
ax = fig.add_subplot[111]
# matplotlib 3.0 you have to use align
ax.bar[indices-width,womenMeans,width,color='b',label='-Ymin',align='edge']
ax.bar[indices,menMeans,width,color='r',label='Ymax',align='edge']
ax.set_xlabel['Test histogram']
plt.show[]
# matplotlib 2.0 [you could avoid using align]
# ax.bar[indices-width,womenMeans,width,color='b',label='-Ymin']
# ax.bar[indices,menMeans,width,color='r',label='Ymax']
Đây là kết quả:
Điều gì sẽ xảy ra nếu các chỉ số của tôi trên trục X của tôi là các giá trị danh nghĩa như tên:
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcGrouped Bar chart. A Bar plot or a Bar Chart has many customizations such as Multiple bar plots, stacked bar plots, horizontal bar charts. Multiple bar charts are generally used for comparing different entities. In this article, plotting multiple bar charts are discussed.
Bàn luậnSimple multiple bar chart
Một biểu đồ nhiều thanh còn được gọi là biểu đồ thanh được nhóm. Một lô thanh hoặc biểu đồ thanh có nhiều tùy chỉnh như nhiều lô thanh, lô thanh xếp chồng, biểu đồ thanh ngang. Nhiều biểu đồ thanh thường được sử dụng để so sánh các thực thể khác nhau. Trong bài viết này, âm mưu nhiều biểu đồ thanh được thảo luận.
Approach:
- Ví dụ 1: Biểu đồ nhiều thanh đơn giản
- Trong ví dụ này, chúng ta sẽ thấy cách vẽ nhiều biểu đồ thanh bằng matplotlib, ở đây chúng ta đang vẽ nhiều biểu đồ thanh để hình dung số lượng nam và nữ trong mỗi nhóm.
- Nhập các thư viện bắt buộc như Numpy để thực hiện các tính toán số với các mảng và matplotlib để trực quan hóa dữ liệu.
- Dữ liệu để vẽ nhiều biểu đồ thanh được đưa vào danh sách.
- Hàm np.arange [] từ thư viện Numpy được sử dụng để tạo ra một loạt các giá trị. Chúng tôi đang tạo các giá trị trục x tùy thuộc vào số lượng nhóm trong ví dụ của chúng tôi.
- Vẽ đồ thị nhiều thanh bằng hàm plt.bar [].
- Để tránh chồng chéo các thanh trong mỗi nhóm, các thanh được dịch chuyển -0.2 đơn vị và +0.2 đơn vị từ trục X.
Code:
Python3
Chiều rộng của các thanh của mỗi nhóm được lấy là 0,4 đơn vị.
Cuối cùng, nhiều bảng xếp hạng thanh cho cả nam và nữ được vẽ trong mỗi nhóm.
import
numpy as np
import
matplotlib.pyplot as plt
X
=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
7=
[
import
0____1010____222222222222import
8=
[
import
2222
numpy as np
9=
import
1import
2import
3
X
2
X
3X
4matplotlib.pyplot as plt
2
import
4___
X
9=
0matplotlib.pyplot as plt
2
=
2
=
3
import
4matplotlib.pyplot as plt
4 import
6matplotlib.pyplot as plt
6import
8import
9=
X
0matplotlib.pyplot as plt
2
X
6X
7matplotlib.pyplot as plt
2Number of Men and Women voted from 2018-2021
Đầu ra:
- Ví dụ 1: Biểu đồ nhiều thanh đơn giản
- Trong ví dụ này, chúng ta sẽ thấy cách vẽ nhiều biểu đồ thanh bằng matplotlib, ở đây chúng ta đang vẽ nhiều biểu đồ thanh để hình dung số lượng nam và nữ trong mỗi nhóm.
- Nhập các thư viện bắt buộc như Numpy để thực hiện các tính toán số với các mảng và matplotlib để trực quan hóa dữ liệu.
- Dữ liệu để vẽ nhiều biểu đồ thanh được đưa vào danh sách.
- Hàm np.arange [] từ thư viện Numpy được sử dụng để tạo ra một loạt các giá trị. Chúng tôi đang tạo các giá trị trục x tùy thuộc vào số lượng nhóm trong ví dụ của chúng tôi.
- Vẽ đồ thị nhiều thanh bằng hàm plt.bar [].
- Để tránh chồng chéo các thanh trong mỗi nhóm, các thanh được dịch chuyển -0.2 đơn vị và +0.2 đơn vị từ trục X.
- Chiều rộng của các thanh của mỗi nhóm được lấy là 0,4 đơn vị.
Code:
Python3
import
=
5
import
=
7
=
8=
[
[
1[
2[
3[
2[
5[
22.
[
9=
[
'Group A'
2[
2'Group A'
4____822
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
00=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
02#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
03=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
05#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
06=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
08#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
09__ #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
11#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
0#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
13#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
06=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
16=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
18#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
0#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
13#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
21=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
23matplotlib.pyplot as plt
2#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
25matplotlib.pyplot as plt
4 #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
27=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
29____10#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
13#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
06=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
16=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
18#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
0#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
13#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
21=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
41matplotlib.pyplot as plt
2#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
25matplotlib.pyplot as plt
4 #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
27=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
29____10X
6
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
47matplotlib.pyplot as plt
2X
9
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
50matplotlib.pyplot as plt
2X
3
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
444____52=
2
=
3
52#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
matplotlib.pyplot as plt
4
54#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
55#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
56__#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
Đầu ra:
Approach:
- Ví dụ3: Điểm số của những người chơi khác nhau vào các ngày khác nhau
- Nhập các thư viện bắt buộc như Numpy để thực hiện các tính toán số với các mảng và matplotlib để trực quan hóa dữ liệu.
- Hàm np.arange [] từ thư viện Numpy được sử dụng để tạo ra một loạt các giá trị [ở đây, 3 giá trị].
- Vẽ đồ thị nhiều thanh bằng hàm plt.bar [] trong thư viện matplotlib. Trong ví dụ này, ngày được vẽ trên trục X và điểm số của người chơi trên trục y.
- Để tránh chồng chéo các thanh trong mỗi nhóm, các thanh được dịch chuyển 0,25 đơn vị từ thanh trước đó.
- Chiều rộng của các thanh của mỗi nhóm được lấy là 0,25 đơn vị với các màu khác nhau.
- Các nhãn trục X và Ticks X được vẽ theo yêu cầu trong trực quan hóa của chúng tôi.
Code:
Python3
import
=
5
import
=
7
Cuối cùng, biểu đồ nhiều thanh cho điểm số của những người chơi khác nhau vào các ngày khác nhau được vẽ.
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
72=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
74#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
06=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
08#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
09__ #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
11#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
0#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
13#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
06=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
16=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
18#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
0#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
25matplotlib.pyplot as plt
4 #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
27=
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
29____10X
3
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
444____52#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
52matplotlib.pyplot as plt
4 #
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
54#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
55#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
56__Đầu ra:
X
3import
34matplotlib.pyplot as plt
2
X
6import
37matplotlib.pyplot as plt
2
X
9import
40matplotlib.pyplot as plt
2
import
42matplotlib.pyplot as plt
4import
44import
45[
2import
47[
2import
49
#
import numpy as np
import matplotlib.pyplot as plt
# The data
womenMeans = [25, 32, 34, 20, 25]
menMeans = [20, 35, 30, 35, 27]
indices = range[len[womenMeans]]
names = ['Asian','European','North Amercian','African','Austrailian','Martian']
# Calculate optimal width
width = np.min[np.diff[indices]]/3.
fig = plt.figure[]
ax = fig.add_subplot[111]
ax.bar[indices-width/2.,womenMeans,width,color='b',label='-Ymin']
ax.bar[indices+width/2.,menMeans,width,color='r',label='Ymax']
#tiks = ax.get_xticks[].tolist[]
ax.axes.set_xticklabels[names]
ax.set_xlabel['Test histogram']
plt.show[]
65import
51import
52[
2import
54[
2import
56import
57
=
3
Output: