Python绘图遇TypeError:tuple下标应为整数/切片而非元组求助
问题:多图绘制时触发TypeError,单独绘制无异常
使用Python从CSV读取数据绘制多子图时,执行代码行axs[0].bar(r-(width*2), Ruby[1:, 7], width=width, color='w', alpha=1)时出现错误:
TypeError: tuple indices must be integers or slices, not tuple
但单独绘制Ruby数据集时无此错误,所有文件单独绘制均正常。相关代码如下:
with open("Ruby\Ruby.csv") as file_name: Ruby= np.genfromtxt(file_name, delimiter=",") with open("Mine Slide\MS.csv") as file_name: MS = np.genfromtxt(file_name, delimiter=",") dates = ['2021\n03 Oct\n15 Oct', '15 Oct\n27 Oct', '27 Oct\n08 Nov', '08 Nov\n20 Nov', '20 Nov\n02 Dec','2021\n02 Dec\n14 Dec'] ifg= [1,2,3,4,5,6] fig, axs = plt.subplots(2, 1) n=6 r=np.arange(n) width= 0.07 axs[0].bar(r-(width*2), Ruby[1:, 8], width=width, color='pink', alpha=0.5) Ruby=axs[0].bar(r-(width*2), Ruby[1:, 4], width=width, label='Ruby Creek', color='pink', alpha=0.5) axs[0].bar(r-(width*2), Ruby[1:, 7], width=width, color='w', alpha=1) axs[0].bar(r-(width*1), MS[1:7, 8], width=width, color='r', alpha=0.5) MS1=axs[0].bar(r-(width*1), MS[1:7, 4], width=width, label='Mine Slide 1', color='r', alpha=0.5) axs[0].bar(r-(width*1), MS[1:7, 7], width=width, color='w', alpha=1) axs[0].bar(r-(width*0), MS[7:13, 8], width=width, color='b', alpha=0.5) MS2=axs[0].bar(r-(width*0), MS[7:13, 4], width=width, label='Mine Slide 2', color='b', alpha=0.5) axs[0].bar(r-(width*0), MS[7:13, 7], width=width, color='w', alpha=1) R2=axs[0].bar(r+(width*3), MS[25:, 4], width=width, label='Ruby 2', color='m', alpha=0.5) axs[0].bar(r+(width*3), MS[25:, 7], width=width, color='w', alpha=1) #axs[0].bar_label(Ruby, padding=1, rotation='vertical') axs[0].bar_label(MS1, padding=1, rotation='vertical') axs[0].bar_label(R2, padding=1, rotation='vertical') axs[0].set_ylim(0, 1) axs[0].set_yticks(np.arange(0, 1, step=0.2)) axs[0].set_xticks(np.arange(0, 6, step=1)) #axs[0].set_xticklabels(('Pre-Failure', 'Pre-Failure', 'Pre-Failure', 'Failure', 'Post-Failure', 'Post-Failure', 'Post-Failure')) axs[0].set_ylabel('Coherence') axs[0].set_xlabel('IFG NO.') axs[1].plot(dates, Ruby[1:, 4], color='pink', label='Ruby Creek') axs[1].plot(dates, MS[1:7, 4], color='r', label='Mine Slide 1') axs[1].plot(dates, MS[7:13, 4], color='b', label='Mine Slide 2') axs[1].plot(dates, MS[25:, 4], color='m', label='Ruby 2') axs[1].title.set_text('November 2021- Asc- Master Varied-Slides-VV') axs[1].set_ylim(0, 1) axs[1].set_yticks(np.arange(0, 1, step=0.2)) axs[1].set_ylabel('Coherence') axs[1].set_xlabel('Dates') axs[1].legend(bbox_to_anchor=(1, 0.5), loc="center left")
问题原因与解决方案
核心原因
你在代码里覆盖了Ruby变量的类型:
- 初始时
Ruby是通过np.genfromtxt读取的numpy数组,支持Ruby[1:,7]这种二维切片操作。 - 但在执行
Ruby=axs[0].bar(r-(width*2), Ruby[1:, 4], ...)后,Ruby被重新赋值为matplotlib的BarContainer对象(条形图容器),它的索引只能是整数,不能用[1:,7]这种元组形式的切片,因此触发TypeError。
单独绘制时你不会执行这段覆盖变量的代码,所以不会报错。
修复方案
把存储条形图容器的变量名改成别的,不要和numpy数组的变量名重复,比如改成ruby_bar:
# 原代码:Ruby=axs[0].bar(...) ruby_bar=axs[0].bar(r-(width*2), Ruby[1:, 4], width=width, label='Ruby Creek', color='pink', alpha=0.5)
如果后续需要给这个条形图加标签,也要把注释里的bar_label(Ruby)改成bar_label(ruby_bar)。
另外,下方子图里的axs[1].plot(dates, Ruby[1:, 4], ...)也依赖原来的numpy数组Ruby,变量被覆盖后这里也会报错,必须修改变量名避免冲突。
内容的提问来源于stack exchange,提问作者Eimilems
相关产品推荐
相关产品推荐

