Repository navigation
Conversation
|
After separating the heatmap from the colorbar I was surprised how clunky it feels to ask the heatmap what the min/max limits are then load them into the colormap. It feels like the API could be improved still 🤔 |
One option would be to grab the information from the You could give a heatmap a Note that if you go the factory method route there are a couple of snakes waiting should the heatmap be modified, I don't know if you want the user to take care of that or if you want to create something (such as a class inheriting from If you go with the observer pattern then that choice is delightfully known as "dead [objects] tell no tales", i.e. the colorbar will never change after its parent heatmap is disposed. However if you use another route, such as checking the parent heatmap has not changed before every render of the colorbar than that choice is simply to prevent the parent heatmap from being disposed by the garbage collector because there is a reference to the parent heatmap held by the colorbar. There is another option with this approach, if you create a weak reference (a reference that does not prohibit garbage collection) then you get a dangling reference, which would create a |
|
I implemented this with the observer pattern in #682. Basically, the end result is you can just do this: Colorbar cb = plt.AddColorbarFromHeatmap(hm);If you want to disable syncing (i.e. create a colorbar from the heatmap and don't modify it on subsequent changes) you can do this: Colorbar cb = plt.AddColorbarFromHeatmap(hm, synced: false); |
|
Hey @Benny121221, thanks for your input! There is a lot of good discussion here. SyncingYou gave a lot of thoughts about how to keep a colorbar and heamap sync'd. At the end of the day, I don't think I'm in favor of this though... It adds a lot of coupling and complexity to this library, all so the end-user doesn't have to write an extra line or two of code in the niche case that they want to change colormaps or tick labels after a heatmap is displayed. You pointed out a lot of "snakes" above and gave some good ideas about how to overcome them, but altogether I think I'm leaning away from this type of syncing in general in an effort to keep the code minimal and well isolated. However, observer pattern would be a good way to accomplish syncing, and the implementation in #682 is a good demonstration of this pattern! Interestingly, a strong argument could be made that the control should the other way around: A colorbar should be a type of Although I don't think we should go down that road at this time, if we did want to pursue syncing I think we should go all-in and have an Simpler API to Create a Heatmap and Colorbar
Maybe we could collapse this into a single method? This could create both items at the same time and initialize the colorbar the appropriate state, and return both in case the user wants to fine-tune something 🤔 var (hm, cb) = plt.AddHeatmapAndColorbar(intensities);Alternative Simpler / Explicit APIColorbar arguments for initialization with min/max labels might make the creation simpler... var hm = plt.AddHeatmap(intensities);
var cb = plt.AddColorbar(hm.Colormap, hm.MinLabel, hm.MaxLabel);An overload in the plot module is possible (below) to mimic var hm = plt.AddHeatmap(intensities);
var cb = plt.AddColorbar(hm); |
|
Of the options you listed my preference would be allow a user to create a colorbar with the ticks up front: plt.AddColorbar(hm.Colormap, hm.MinLabel, hm.MaxLabel);Or to create a colorbar from a heatmap: plt.AddColorbar(hm);On the topic of syncing, I mostly agree. While it is convenient, it's not a likely issue to come up, and if it does it's not difficult for a user to maintain the two objects with the same state. If they were so inclined they could even use the observer pattern with something like a decorator class. |
@Benny121221 I created this method like you suggested and it really simplified the cookbook examples! I added a new cookbook section for Colorbar too. There is certainly room for improvement/refinement for both |
Previously colorbar rendering was achieved inside the heatmap module. This PR gives colorbar its own plot type, and
AddColorbar()can be called on any plot. This addresses #678The colorbar could benefit from continued refinement/refactoring (especially the tick system), but this is a strong start.
This PR also makes some changes suggested by #680