我想将多个分组的条形图合并为一张图,如下图所示。 将条形图分组到单个图中
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1', 'G2', 'G3']
yesterday_test1_mean = [20, 12, 23]
yesterday_test2_mean = [21, 14, 25]
today_test1_mean = [18, 10, 12]
today_test2_mean = [13, 13, 9]
Run Code Online (Sandbox Code Playgroud)
首先,我创建了每个分组条形图plt.subplots()
x = np.arange(len(labels))
width = 0.3
fig1, ax = plt.subplots()
rects1 = ax.bar(x-width/2, yesterday_test1_mean, width)
rects2 = ax.bar(x+width/2, yesterday_test2_mean, width)
fig2, ax = plt.subplots()
rects3 = ax.bar(x-width/2, today_test1_mean, width)
rects4 = ax.bar(x+width/2, today_test2_mean, width)
Run Code Online (Sandbox Code Playgroud)
然后,我尝试add_subplot将fig1和fig2作为新图形中的新轴。 …
我正在尝试解决标题中的问题,但出现 IndexError。
def count_zeros(number:int) -> int:
number = str(number)
i = 0
j = -1
while number[j] == '0'
i += 1
j += -1
return i
Run Code Online (Sandbox Code Playgroud)
仅当数字为 0 时,此代码才会吐出 IndexError。我不知道为什么。
>>>count_zeros(0)
IndexError Traceback (most recent call last)
<ipython-input-45-688099e7700c> in <module>
----> 1 end_zeros(0)
<ipython-input-43-181de92af60c> in end_zeros(number)
4 i = 0
5
----> 6 while number[j] == '0':
7 i += 1
8 j -= 1
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)