-
Notifications
You must be signed in to change notification settings - Fork 51
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
Allow getitem from a cycler #27
Comments
Cycler already has some support for I am not convinced that using index is clearer than using styles = zip(data_cycler, fit_cycler)
values = zip(data_list, fit_list)
for vals, cys in zip(styles, values):
for v, c in zip(vals, cys):
ax.plot('x', 'y', data=v, **c) is a bit clearer (and avoids what I assume is the typo where you meant To do it efficiently you would probably have to hold a list of the transposed cycler (ex That said, we would have use of this functionality upstream in mpl. I am -0 on this, I won't merge it, but will not block someone else from doing so. |
@tacaswell yep, I fixed the typo in the example. Anyway, it was just an example. The point is, there are legitimate situations where indexing a cycler can be convenient. Another example more similar to what I'm doing ATM. I keep a few datatsets in dictionaries whose keys (the same for all dict) are parameters. Would be nice to be able to plot them like this (let's pretend I still have fit and data curves): for i, (param, data) in enumerate(sorted(data_dict.items())):
ax.plot('x', 'y', data=data, **data_cycler[i])
ax.plot(data.x, model_func(data.x **fit_dict[param]), **fit_cycler[i])
ax.text(1, 0.9 - i*0.1, 'parameter = %d, tau = %.1fs' % (param, fit_dict[param]['tau'])) instead of: for i, (param, datasty, fitsty) in enumerate(zip(sorted(data_dict), data_cycler, fit_cycler)):
ax.plot('x', 'y', data=data_dict[param], **datasty)
ax.plot(data.x, model_func(data.x **fit_dict[param]), **fitsty)
ax.text(1, 0.9 - i*0.1, 'parameter = %d, tau = %.1fs' % (param, fit_dict[param]['tau'])) All these "style" variables in the latter example are just visual noise, distracting from the important thing: the data I'm looping upon. |
The example at https://github.com/matplotlib/cycler/blob/master/doc/source/index.rst#persistent-cycles (which needs to get rebuilt and pushed to the main page) I think captures your second use case in a nicer way. |
I think that the defaultdict construction addresses this use case. Do we want wrap it in a function and ship it with cycler? I am leaning towards yes. |
Function to generate a default dict with a cycler bound to it. This functionality is currently in the documentation as an example. Closes matplotlib#27
Function to generate a default dict with a cycler bound to it. This functionality is currently in the documentation as an example. Closes matplotlib#27
It would be useful to add
__getitem__
functionality tocycler
.The use case is a "crowded" for loop where you want to get the cycler style without using a loop variable. Something like:
In this case the index
i
is computed anyway, so using it for the cyclers would be handy, instead of adding the cycler insidezip()
.Ideally
mycycler[i]
should return the item with indexi % len(mycycler)
.I haven't looked at the code yet. Would this be possible? Would a PR be welcome?
The text was updated successfully, but these errors were encountered: