Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Locators raise unclear exceptions on MappingView input #7527
When dict_values are passed to plt.xticks(dict_values), Matplotlib raises non obvious exception and corresponding traceback:
Is it expected to fail? And if so, why such a strange error? Because len(dict_values) is ok.
p.s.: I'm new to Python and its community. Is it the right place to post such questions?
@story645 Sorry, I can`t catch your idea. If I cast dict_values the order will also be unpredictable. I agree that it is not good idea to use them (In my case, for positions the order really doesn't matter).
But the exception looks very strange (because
list(dict_values) will work, but now I'd really like to know what you're trying to do.
Support for dict_values as input into the plotting routines is baked into 2.0, as is direct support for textual data. I think that negates adding support for using dict_values directly in labels (though this may not be hard to do) as that's a thing people shouldn't ever be doing. But hmm, I would agree that maybe the message could be clearer.
Also, mappingviews don't have a fixed size as they're kind of just pointers to the dictionary and so get updated when the dict does , whereas the old dict.views() returned list copies of the current state of the keys https://docs.python.org/3/library/stdtypes.html#dict-views
@story645 , Nothing serious, I was trying to execute examples from a book:)
@tacaswell , I believe that Exceptions are my best friends, and here the message that does not tell anything. Also there is some inconsistency, because in some places its OK and not in another one.
plt.xticks(np.arange(len(dct))+0.4, dct.keys()) #here dict_keys is ok!!! plt.yticks(dct.values()) #but here it is ERROR!!!
The argument order for both is plt.xticks(locs, labels) and plt.yticks(locs, labels). Different code handles where the ticks are located and how to label it, and the support/lack thereof is due to how the code is structured.
The code to create labels (the thing that works) creates list by iterating over the input and appending, and so anything that supports iteration (including dict_views) works.
The code for creating ticks is the rabbit hole that error message spits out. This also fails:
changed the title
Tick Locators Fail on MappingView Objects with Unclear Error Message
Nov 29, 2016
Closing because I would consider that ultimately all python(3) users will need to learn that dict views are, well, not lists, and I don't think it particularly makes sense to try catching all kinds of unsupported input to rephrase the error message.