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
How to add a colorbar legend next to a calmap.calendarplot? #9
Comments
Hi @BreitA, Yes, this is something that I would like to have. Currently, I don't think it's even possible to do this manually outside of calmap after calling it. An implementation in diff --git a/calmap/__init__.py b/calmap/__init__.py
index 1c53a8c..fddca78 100644
--- a/calmap/__init__.py
+++ b/calmap/__init__.py
@@ -181,7 +181,8 @@ def yearplot(data, year=None, how='sum', vmin=None, vmax=None, cmap='Reds',
# Draw heatmap.
kwargs['linewidth'] = linewidth
kwargs['edgecolors'] = linecolor
- ax.pcolormesh(plot_data, vmin=vmin, vmax=vmax, cmap=cmap, **kwargs)
+ mesh = ax.pcolormesh(plot_data, vmin=vmin, vmax=vmax, cmap=cmap, **kwargs)
+ ax.figure.colorbar(mesh)
# Limit heatmap to our data.
ax.set(xlim=(0, plot_data.shape[1]), ylim=(0, plot_data.shape[0])) Some quick thoughts:
Not sure when I'll have time to look into this. Would welcome a PR though :) |
I cooked something this evening that works, I will send you what I've done when it's clean. |
Okay so it's very "homemade" but does the job. I'm very bad with matplotlib so I'm sure there is a more elegant way to do the stuff but it works wonders for me atm. as you can see I didt it as an external function to call after the calmap plot.
|
@BreitA Thanks a lot for sharing your code. I don't have the time to work on it now, but I would like to include something like you're showing here in the future. So I'm leaving this issue open, thanks again 👍 |
Hi, I was searching for the same stuff and I didn't think about looking here in the issues! I answer to a question on stackoverflow python 2.7 - add a colorbar to a calmap plot and I think it's yours, @BreitA . My solution is to pick the children of the axis returned by yearplot and stick the colorbar to this mappable : after digging in the code, it is the second call to ax.quadmesh . So the code would be: fig,ax=calmap.calendarplot(df['data'],
fillcolor='grey', linewidth=0,cmap='RdYlGn',
fig_kws=dict(figsize=(17,8)))
fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist()) and even simpler for a single fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
cax = calmap.yearplot(df, year=2014, ax=ax, cmap='YlGn')
fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal') A good start would be to return the reference to the actual graphic object that contains the data in the |
Thanks @kidpixo for commenting here and linking back to the SO thread! I'm interested in adding this or making it easier to do it yourself, but time limited at the moment. |
Thank you for the package, @martijnvermaat ! A solution would be to alter the return of I can also think about an helper function, like Something more elaborated for |
Thanks for the solution provided @kidpixo , will try it when I have the time ! And yes the Stackoverflow was mine and then I ended up asking the question here. |
@BreitA you are welcome! Could you please accept my solution on stackoverflow? |
Yeah I just did! I tested it and it is exactly what I was looking for and it works great! |
Nice to hear :-D if you have time to contribute or to publish some code of your solution will be great! |
Honestly at this point your solution is much more elegant and show a better understanding of matplotlib than I have done with my homemade solution. Thanks again. |
|
Hi, if this problem still exists and you'd like to create a PR to fix it please direct it to https://github.com/MarvinT/calmap/ |
Thank you for creating the issue. Unfortunately I don't have the time to maintain this project. As per @MarvinT 's comment, please see https://github.com/MarvinT/calmap/ instead. |
Hi,
I'm currently heavily relying on your module to plot calendar heatmap. Your module is great but I wonder
If you know a simple way to put a colorbar for the whole figure on the left of the calendar heatmap figure.
It could also be a neat feature in your next iteration on the module to have an option to put automatically the colorbar when creating the heatmap.
The text was updated successfully, but these errors were encountered: