8000 Error Handling of Non-Ints/Floats for postion of xticks · Issue #10147 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Error Handling of Non-Ints/Floats for postion of xticks #10147
Closed
@gurkpet

Description

@gurkpet

This appears to be the underlying cause of ticket #10130

I think this is an instance of inappropriate error handling in matplotlib.

I have been able to reproduce this running python 3.5 and matplotlib 2.1.1

The following code can reproduce the error and requires plt.close() to clean out the pipeline before more plotting can be done:

import matplotlib.pyplot as plt
data = [1,2,3,4,5]
ticks = ['any string']
plt.plot(data)
plt.xticks(ticks)
plt.show() 
AttributeError: 'NoneType' object has no attribute 'seq'

The error is set-up by assigning non int/floats to the first argument of xticks.

In:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/category.py

StrCategoryConverter.convert seems to exist for the handling of strings but it doesn't seem to be handling this particular scenario properly.

on line 44 if you add:

if axis.unit_data is None:
     axis.unit_data = UnitData([0])

It's a quick fix that makes it work but probably not the best solution. Its also of note that it is not just strings but data types other than int and float in the first argument of xticks will produce the same error.

While

plt.close()

Can be used to clear out the errors in jupyter notebook but I think that the error handling still can be improved.

An alternative fix could be to prevent xticks from assigning bad values like this in
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/pyplot.py
on lines 1662-1667:

elif len(args)==1:
        if not any(isinstance(x, int) | isinstance(x, float) for x in args[0]):
            raise TypeError('First argument must be type int or float')
        locs = ax.set_xticks(args[0])
        labels = ax.get_xticklabels()
        
elif len(args)==2:
        if not any(isinstance(x, int) | isinstance(x, float) for x in args[0]):
            raise TypeError('First argument must be type int or float')
        locs = ax.set_xticks(args[0])
        labels = ax.set_xticklabels(args[1], **kwargs)   

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0