-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
eventplot throws exception when using color different than one of {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'} #8193
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
Comments
You are lucky: the Matplotlib version that you are using is from the future! 2.0.1 is not even out yet ;). From a quick look at the code, I would say that this reveals some kind of inconsistency in the API. The docstring says that only an RGBA (or a list of RGBA) value(s) should be provided to
The same is present in the docstring of I suspect that in the case of passing a string longer than a single character, each character is considered as a color, which causes two issues:
|
A workaround may be to manually handle the conversion into RGBA: from matplotlib.colors import to_rgba_array
plt.eventplot([1,2], colors=to_rgba_array('C0')) # color= is also possible |
Thanks for the answer @afvincent. My mistake with Matplotlib version. It is 2.0.0. And also sorry with the confusion between |
@aweinstein No worries :). Funnily |
To other devs I wonder if simply replacing this line in if isinstance(colors, six.string_types) or not iterable(colors): would fix the issue (strings are iterable, so currently the next line will be skipped and we will not pass a list of valid color names to Besides, I do not understand why the docstrings precise that only RGBA values should be given. Under the hood, it seems to me that everything will be OK we a list of any type of valid colors (RGBA values, single characters, full string or Cn names), as in the end everyhting goes through |
I would do something like:
|
Err, documentation issues aside, a much simpler workaround is to pass a list: |
@anntzer What is the advantage of using I quickly tested the following fix: From 880208469cc894285e2f47b5768e4617281c8266 Mon Sep 17 00:00:00 2001
From: Adrien F Vincent <vincent.adrien@gmail.com>
Date: Sat, 4 Mar 2017 11:08:24 +0100
Subject: [PATCH] naive fix for issue 8193
---
lib/matplotlib/axes/_axes.py | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index 5ffd8a7..e4a617b 100644
--- a/lib/matplotlib/axes/_axes.py
+++ b/lib/matplotlib/axes/_axes.py
@@ -1193,6 +1193,15 @@ or tuple of floats
lineoffsets = [None]
if len(colors) == 0:
colors = [None]
+ try:
+ # Early conversion of the colors into RGBA values to take care
+ # of cases like colors='0.5' or colors='C1'. (Issue #8193)
+ colors = mcolors.to_rgba_array(colors)
+ except ValueError:
+ # Will fail if any element of *colors* is None. But as long
+ # as len(colors) == 1 or len(positions), the rest of the
+ # code should process *colors* properly.
+ pass
if len(lineoffsets) == 1 and len(positions) != 1:
lineoffsets = np.tile(lineoffsets, len(positions))
--
2.10.2
It seems to do the job. For example, the following script import matplotlib.pyplot as plt
fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(9.6, 4.8))
events = ((1, 4), (2, 5), (3, 6))
colors_A = "C2" # a realistic case
colors_B = "rkb" # kind of a corner case
colors_C = ["tab:red", None, "BlAcK"] # another weird case
for ax, colors in [(ax0, colors_A), (ax1, colors_B), (ax2, colors_C)]:
ax.eventplot(events, colors=colors)
ax.set_title("colors={c}".format(c=colors))
# Cosmeticks
ax.set_xticks([])
ax.set_yticks([])
plt.show() And it does not seem to break any of the |
|
For sure, it seems appealing ^^. However, I am not sure that it would exactly fit into the scope of a PR aimed at fixing the current issue: from what I understand, more or less all the parameters of At the moment, I have a PR on its way, just for the current issue, based on the “naive” fix. I am planning to add some kind of test and to perform some (at least minimal) overhaul of the relevant docstrings (the ones for |
Do you want to review #7562? :-) (which does not fix eventplot, though, I believe). |
Wait, #7562 is so huge! But I'll see what I can do (at least for the parts I can understand) ;). |
fixed by #8861. |
Bug report
The user guide (http://matplotlib.org/users/colors.html) specifies different ways to specify the
color
parameter. However,eventplot
throws an exception when using a color different than the one in {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}, such ascolor='C0'
orcolor='0.5'
.Code for reproduction
Actual outcome
Matplotlib version
The text was updated successfully, but these errors were encountered: