rougier / scientific-visualization-book

An open access book on scientific visualization using python and matplotlib
https://www.labri.fr/perso/nrougier/
Other
10.72k stars 993 forks source link

add transforms-hist-alternative.py #78

Closed leejjoon closed 1 year ago

leejjoon commented 2 years ago

First of all, great book!

In "transforms-hist.py", using floating_axes is a bit cumbersome as you would already know (FWIW, I was the original author of axis_artist and axes_grid1 toolkit). My recommendation is to use parasite_axes with auxiliary transform (which defines the transform from the parasite_axes to the host_axes). Still, you need to create two additional axes, one to draw floating axis, another to draw histogram. However, I guess the logic is simpler and involves less math. For example, P2, P3 and P4 which were originally drawn in ax1, are now simply drawn using vlines and fill_between in the parasite_axes.

I understand that changing the code this way is not enough and the text also need to be changed. I just wanted to suggest an alternative solution.

For the question in the original comment at line 151,

# This auxiliary axis is necessary to draw stuff (no real idea why)
ax2_aux = ax2.get_aux_axes(transform)

axis_artist was originally designed to show images in the rectangular (pixel) coordinate and overlay the curvelinear coordinates upon them (mainly for astronomical images). So the transData of ax2 was meant to be the rectangular coordinate with which you can show images in their pixel coordinates. This could be confusing but this was the design decision I made at that time.

Anyhow, I think one can create another axes class (derived from floating_axes and/or parasite_axes) for use cases like this, which could be much easier to use. I may give it a try when I have more time.

(EDIT: The original PR tried to patch the "transform-hist.py". The revised PR add a new file "transform-hist-alternative.py" instead.)

rougier commented 2 years ago

Nice, thank you for the patch. I agree the current solution and bit complex such that I prefer to use your solution. By, I guess you get the same result or are there some discrepancies? Also, ideally, if you can, the descriptive text in the book would need to be adapted.

leejjoon commented 2 years ago

The result using my code would not be identical to the original. In the new code, the height of the rotated axes is set by the transform (transform_to_rotated). Also the original code is drawing the histogram of -Z1 @ PC1 and I was not sure about its intention. In the new code, I simply draw histogram of projected values of Z1 along the x-axis of the rotated axes (i.e., perpendicular vector to PC1).

rougier commented 2 years ago

So mayeb it would be worth just to add your solution alongside mine with a trailing _alternative?

leejjoon commented 2 years ago

Yes, that sounds a good idea. I will change the PR.

leejjoon commented 2 years ago

I have reverted the change in my original PR. Instead, I have added a new file "transform-hist-alternative.py". The content is same.

rougier commented 1 year ago

Thanks!