Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix on color handling for showProjection #1070

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

jamesmkrieger
Copy link
Contributor

In the previous version, a 1D array or tuple with the same length as the number of data points would become a matrix with that length in both directions, and indict would get messed up. This was to account for arrays or tuples with RGB or RGBA values.

@jamesmkrieger jamesmkrieger force-pushed the devel-5 branch 3 times, most recently from 3da94bb to 133a7cc Compare June 10, 2020 11:30
Copy link
Collaborator

@SHZ66 SHZ66 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import checker from ~matplotlib.pyplot.scatter or make it clearer in the documentation about the input format

@jamesmkrieger jamesmkrieger marked this pull request as draft July 15, 2020 11:09
prody/dynamics/plotting.py Outdated Show resolved Hide resolved
prody/dynamics/plotting.py Outdated Show resolved Hide resolved
colors = [colors_dict[label] for label in labels]

if isinstance(colors, list):
if len(colors) != num and not is_color_like(colors):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need and not is_color_like(colors) here since if it was color-like, line 2371 would have converted it to a list of colors (of num) already (correct me if I am wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, yes, I agree

prody/dynamics/plotting.py Outdated Show resolved Hide resolved
if np.any([not is_color_like(color) for color in colors]):
if not allowNumbers:
raise ValueError('each element of colors should satisfy matplotlib color rules')
elif np.any([not isinstance(color, Number) for color in colors]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above too

prody/dynamics/plotting.py Outdated Show resolved Hide resolved
@jamesmkrieger jamesmkrieger requested a review from SHZ66 July 30, 2024 10:54
raise ValueError('each element of colors should be a number or satisfy matplotlib color rules')

if not isinstance(color, type(colors[0])):
raise TypeError('each element of colors should have the same type')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary? Couldn't the matplotlib function handle colors defined in different ways in the same list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I guess it probably could


colors_dict = {}

if is_color_like(colors) or colors is None or isinstance(colors, Number):
Copy link
Collaborator

@SHZ66 SHZ66 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should colors is None be part of this? Wouldn't a list of None's fail the check down below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would fail so I added it down there too, because we do want to allow it

if not isinstance(color, type(colors[0])):
raise TypeError('each element of colors should have the same type')
else:
raise TypeError('colors should be a colour spec or convertible to a list of color specs')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: colour spec -> color spec for style consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

colors = [colors_dict[label] for label in labels]
else:
raise TypeError('color must be a string or a list or a dict if labels are provided')
colors, colors_dict = checkColors(colors, num, labels)

if labels is not None and len(colors_dict) == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this section can go away and checkColors doesn't need to return color_dict.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a case where we need colors_dict on line 317 where we make a line graph. We'll have to find a way to adjust that.


for color in colors:
if not is_color_like(color):
if not allowNumbers:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if allowNumbers, you may need to convert the number to the color in cycle like this:

cycle_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
color = cycle_colors[color % len(cycle_colors)]

Copy link
Collaborator

@SHZ66 SHZ66 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the entire loop may look like this:

if isinstance(colors, list):
      if len(colors) != num:
          raise ValueError('colors should have the length of the set to be colored or satisfy matplotlib color rules')

      for i, color in enumerate(colors):
          if not is_color_like(color):
              if allowNumbers:
                  cycle_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
                  colors[i] = cycle_colors[color % len(cycle_colors)]
              else:
                  raise ValueError('each element of colors should satisfy matplotlib color rules')
       ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a conversion of the numbers with the color cycle somewhere else, but yes, we could maybe move it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we use matplotlib.colors.Normalize instead to link up with the cmap. This is on lines 356 and 428

This could somehow be incorporated too though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants