扁钢x轴打印与x轴标签不一致(python中的matplotlib)

2024-06-02 08:13:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个条形图,但是我的x轴和x轴标签没有对齐。。你知道吗

   import matplotlib.pyplot as plt
   import numpy as np
   #import matplotlib.patches as mpatches

   residue_distb = np.loadtxt('Aggregated AA Probabilities Final.csv',                                         
                               delimiter=',', skiprows = 1, usecols = (1))
   print residue_distb
   x = np.arange(len(residue_distb))

   bar_width = 0.4
   plt.bar(x, residue_distb, width=bar_width, color = 'blue')

   aminoacids = np.loadtxt('Aggregated AA Probabilities Final.csv',        
                            dtype='str', delimiter=',', skiprows = 1, usecols = (0))

   plt.xticks (x + bar_width*2, aminoacids)
   plt.xticks(rotation=90)

我认为问题是x在0-19之间,而不是1-20之间:

   x = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

这意味着我的最后一个标签没有值,而我的第一个值没有标签。 我试着做一个列表理解,为每个值加一个,但是当我再次绘制时,它会绘制数字,而不是x轴标签。你知道吗

有办法改变这个吗? Output plot


Tags: importmatplotlibasnpbarplt标签width