你的意思是这样的?
import matplotlib.pyplot as plt
# You can use this to create a figure and axes in one line
# If you want to make 4 plots in a square configuration you'd need
# fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2,ncols=2,figsize=(15,6))
# Google how to do multiplots in matplotlib.
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(15,6))
x_axis_1 = ['A','B','C']
y_axis_1 = [5,10,15]
y_axis_2 = [7,3,4]
ax1.bar(x_axis_1, y_axis_1)
ax2.bar(x_axis_1, y_axis_2)
plt.show()
PS:在您的代码中包含 import 语句。不得不自己查看您正在使用的模块,这很烦人。
PSS:在 python 列表上使用减号永远不会起作用,即使它是用数字填充的。如果你想做那种事情,你需要 numpy 数组:
import numpy as np
array = np.array([1,2,3,2,1])
print(array-0.2)
编辑正确的版本:
原来如此:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(15,6))
# you need to define a bar width for alignment later on
bar_width = 0.35
# Those are just labels, not the positions
x_axis_labels = ['A','B','C']
# Those are just positions, not labels
x_axis_1 = np.arange(len(x_axis_labels))
y_axis_1 = np.array([5,10,15])
y_axis_2 = np.array([7,3,4])
ax.bar(x_axis_1, y_axis_1,bar_width)
ax.bar(x_axis_1+bar_width, y_axis_2,bar_width)
# This sets the number of ticks to the number of labels.
# It also aligns the tick positions with the center of the bars. Notice the + bar_width/2
ax.set_xticks(np.arange(len(x_axis_labels))+bar_width/2)
# This sets the three ticks, which are now at the right positions, to your x_axis_labels
ax.set_xticklabels(x_axis_labels)
plt.show()
PSSS:尝试更准确地表述您的问题。我很难理解你的意思。这个问题也是这个问题的两倍:Python Create Bar Chart Comparing 2 sets of data。
PSSSS:业力请。我想达到 420 分。