Matplotlib上出现未知属性Ticks错误



当我试图将x轴的标签更改为ages_x的标签时,遇到了一个错误。目前,它是x_indexes,这是ages_x的长度。这是我的代码:

import numpy as np
from matplotlib import pyplot as plt

plt.style.use('fivethirtyeight')
ages_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
x_indexes=np.arange(len(ages_x))
width= 0.25
dev_y = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
plt.bar(x_indexes, dev_y, width=width, color= '#444444', label= 'All Devs')
py_dev_y = [45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640]
plt.bar(x_indexes - width, py_dev_y, width=width,  label= 'Python')
js_dev_y = [37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583]
plt.bar(x_indexes + width, js_dev_y, width=width,  label= 'JavaScript')

plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.legend()
plt.xticks(ticks=x_indexes, labels= ages_x)
plt.tight_layout()

plt.show()

错误如下:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-69-ad69a2071340> in <module>()
24 plt.ylabel('Median Salary (USD)')
25 plt.legend()
---> 26 plt.xticks(ticks=x_indexes, labels= ages_x)
27 plt.tight_layout()
28 
/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py in xticks(*args, **kwargs)
1705     if len(kwargs):
1706         for l in labels:
-> 1707             l.update(kwargs)
1708 
1709     return locs, silent_list('Text xticklabel', labels)
/opt/conda/lib/python3.6/site-packages/matplotlib/text.py in update(self, kwargs)
241         """
242         bbox = kwargs.pop('bbox', None)
--> 243         super(Text, self).update(kwargs)
244         if bbox:
245             self.set_bbox(bbox)  # depends on font properties
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in update(self, props)
883         try:
884             ret = [_update_property(self, k, v)
--> 885                    for k, v in props.items()]
886         finally:
887             self.eventson = store
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in <listcomp>(.0)
883         try:
884             ret = [_update_property(self, k, v)
--> 885                    for k, v in props.items()]
886         finally:
887             self.eventson = store
/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in _update_property(self, k, v)
876                 func = getattr(self, 'set_' + k, None)
877                 if func is None or not six.callable(func):
--> 878                     raise AttributeError('Unknown property %s' % k)
879                 return func(v)
880 
AttributeError: Unknown property ticks

我认为您使用的是Matplotlib 的旧版本

更新到更新的版本,您的代码应该运行良好。我刚刚在matplotlib 3.3.2上运行了您的代码,它运行得很好。