小编use*_*317的帖子

如何在Python中创建分组条形图的子图

我想将多个分组的条形图合并为一张图,如下图所示。 将条形图分组到单个图中

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_subplotfig1fig2作为新图形中的新轴。 …

python matplotlib

6
推荐指数
1
解决办法
6210
查看次数

找出数字最后有多少个零(数字为 0 时字符串索引超出范围错误)

我正在尝试解决标题中的问题,但出现 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)

python

2
推荐指数
1
解决办法
415
查看次数

标签 统计

python ×2

matplotlib ×1