Skip to content

plot histogram with class bins#216

Merged
knaaptime merged 14 commits into
pysal:mainfrom
knaaptime:hist
Jul 3, 2024
Merged

plot histogram with class bins#216
knaaptime merged 14 commits into
pysal:mainfrom
knaaptime:hist

Conversation

@knaaptime
Copy link
Copy Markdown
Member

@knaaptime knaaptime commented Jun 19, 2024

wanted to gauge interest in adding a method to Classifier that plots the histogram of y with class bins a la desktop GIS

Screenshot 2024-06-18 at 9 23 12 PM

pandas and seaborn are optional dependencies but usually available in a pysal environment. If we take the coloring logic from #211 I think the legendgram could also be a method on Classifier (without depending on palettable), which would be pretty handy. Will add a test if folks are interested

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.6%. Comparing base (88c6549) to head (61c7c25).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #216     +/-   ##
=======================================
+ Coverage   89.4%   89.6%   +0.1%     
=======================================
  Files          8       8             
  Lines       1088    1101     +13     
=======================================
+ Hits         973     986     +13     
  Misses       115     115             
Files Coverage Δ
mapclassify/classifiers.py 88.6% <100.0%> (+0.2%) ⬆️

@martinfleis
Copy link
Copy Markdown
Member

Nice, I like this and the idea of moving legendgram as well.

I would probably just try to add only maptlotlib as optional dependency. You can easily use plt.hist instead of using pandas and if seaborn is needed only for a minor cosmetic change, you can easily call sns.despine() yourself.

@knaaptime
Copy link
Copy Markdown
Member Author

cool will do. considered dropping the seaborn from the beginning, but despine really makes a difference in how the plot appears to me. Can drop it and add a test

@martinfleis
Copy link
Copy Markdown
Member

I don't think we need seaborn to remove spines :).

ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)

from https://matplotlib.org/3.4.3/gallery/ticks_and_spines/spines_dropped.html

@knaaptime
Copy link
Copy Markdown
Member Author

oh right :P thanks

@knaaptime
Copy link
Copy Markdown
Member Author

plt.hist doesnt take or return an axes though (so the ax.vline doesn't work and isn't as composable otherwise). I think I'll keep it as pandas unless you have a different implementation?

@martinfleis
Copy link
Copy Markdown
Member

Like this

rng = np.random.default_rng(seed=0)
normal = rng.normal(size=1000)

fig, ax = plt.subplots()

ax.hist(normal)

lim = ax.get_ylim()[1]
ax.vlines(-1, 0, lim, color="k")
ax.spines.right.set_visible(False)
ax.spines.top.set_visible(False)

@knaaptime
Copy link
Copy Markdown
Member Author

knaaptime commented Jun 26, 2024

ah, i see. I was doing plt.hist not ax.hist. Thanks!

Comment thread mapclassify/classifiers.py Outdated
Comment thread mapclassify/classifiers.py Outdated
Copy link
Copy Markdown
Member

@jGaboardi jGaboardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy!

@knaaptime knaaptime merged commit af76ab3 into pysal:main Jul 3, 2024
@knaaptime knaaptime deleted the hist branch July 3, 2024 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants