Skip to content

Unhelpful ValueError raised when color array is incorrect format #15727

Closed

Description

@TTitcombe

Bug report

Bug summary
When colors passed to a plot are provided as tuples in the range [0, 255] instead of [0, 1], the error raised relates to the shape of the data (which is correct), not the values of the data.

Code for reproduction

import matplotlib.pyplot as plt

plt.scatter([0, 1], [0, 1], c=[(255, 0, 0), (0, 255, 0)])

Actual outcome

ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 2, 'y' with size 2.

Edit:

ValueError: RGBA values should be within 0-1 range

is also raised, however it is less prominent than the "size" error, which should not be raised at all

Expected outcome
A single, specific error message:

ValueError: RGBA values should be within 0-1 range

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.1.2
  • Matplotlib backend: Qt5Agg
  • Python version: 3.7.3

Installed matplotlib from conda

Activity

  1. TTitcombe commented on Nov 20, 2019

    @TTitcombe
    Author

    The correct error is being raised from colors = mcolors.to_rgba_array(c) in _axes.py. However, this is caught in _axes.py and the incorrect ValueError is raised here.

    The incorrect error is raised because valid_shape is False, which is being set here. This is caused by c.size == xsize evaluating to False when c is an array of tuples because c.size will be number of tuple X size of tuple, so for c=[(255, 0, 0), (0, 255, 0)], c.size == 6 when that statement is intended to check if it is 2.

    I haven't had the time to work out exactly what the intention of this bit of the code is, so have not yet found a solution.

  2. github-actions commented on Jun 30, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  3. QuLogic commented on Jun 30, 2023

    @QuLogic
    Member

    This was fixed by #17245 in 3.3.0.

  4. added this to the v3.3.0 milestone on Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions