Reputation: 159
I ran into an error when I tried to change the label of my x axis to be that of ages_x
. Currently, it's x_indexes
, which is the length of the ages_x
. Here's my code:
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()
Here's the error:
---------------------------------------------------------------------------
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
Upvotes: 1
Views: 1045
Reputation: 1070
I think you are using an old verison of Matplotlib
Update to a newer version and your code should run fine. I just ran your code on matplotlib 3.3.2 and it works fine.
Upvotes: 2