Skip to content
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

working moran plot func #797

Merged
merged 4 commits into from May 21, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 13 additions & 23 deletions pysal/contrib/viz/plot.py
@@ -1,6 +1,5 @@
"""
Moran Plot using PySAL and Matplotlib

Canned Views using PySAL and Matplotlib
"""

__author__ = "Marynia Kolak <marynia.kolak@gmail.com>"
Expand All @@ -15,42 +14,33 @@ def moran(var, w, xlabel='', ylabel='', title='', custom=(5,5)):
'''
Produce basic Moran Plot
...

Arguments
---------

var : array
var : array
values of variable
w : array
values of spatial weight
w : spatial weight
'''
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to add a Returns section explaining what the output is?



## really shouldn't have w transformed here -- error if doesn't match size? (MK)
w.transform = 'r'
w.transform = 'r'
slag = ps.lag_spatial(w, var)

## Z-Score standardisation -- again, how to grab zx and zy from Moran call? (MK)
y_std = (var - var.mean())/var.std()
yl_std = (slag - slag.mean())/slag.std()
zx = (var - var.mean())/var.std()
zy = (slag - slag.mean())/slag.std()

fit = ps.spreg.OLS(zx[:, None], zy[:,None])

## Customize plot
fig1 = plt.figure(figsize=custom)
plt.xlabel(xlabel, fontsize=20)
plt.ylabel(ylabel, fontsize=20)
plt.suptitle(title, fontsize=30)
plt.ylabel(ylabel, fontsize=20)
plt.suptitle(title, fontsize=30)

plt.scatter(y_std, yl_std, s=60, color='k', alpha=.6)

## Add Moran line here -- how much do we assume has been done so far?
plt.scatter(zx, zy, s=60, color='k', alpha=.6)
plot(zy, fit.predy, color='r')

Copy link
Member

Choose a reason for hiding this comment

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

We might want to add a linear (and potentially non-linear) fit line. Also, maybe we could add a legend for that line that includes the coefficient for Moran's I (as it's its slope)?

At the very least, I'd add the linear fit and maybe introduce an option for a LOESS line, although that'd make us depend on statsmodels.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely, the idea with this first pass is just to test the waters and
see what would work! It's still in progress -- you can see my notebook
kicking around some ideas and configs here
https://github.com/Makosak/PySalViz/blob/master/Untitled%20Folder/Scatter%20Plots%20-%20Moran.ipynb.
so:

  • Module name: renaming to 'canned' as a master would be fine -- i'll
    adjust that for the next round. (or 'cooked' ? ;P i guess there's time to
    consider options.)
  • *Default style: *yup, the default style is not terribly pretty in
    matplotlibs. I was trying to keep it to matplotlib for now following the
    viz project specs, and to reduce dependencies, but would it make sense to
    have a parallel style in seaborn? shouldn't be too difficult, and would add
    some options.. (Also, check out the 4-color matplotlib quadrant figure in
    the notebook above...)
  • *kwargs: *these I was reserving for title='', xlabel='', ylabel='',
    and figsize() following matplotlib logic -- see comments in the notebook
    link above. I may be totally misusing the term 'kwargs' here so apologies;
    still green.. I haven't added them yet in this version but just to get
    something out! These would be optional, but a way to enhance the visual if
    you want it. Maybe we could connect seaborn through that, for a more
    advanced user? like seaborn=false will give you a matplotlib default
    object, but if seaborn=true then we assume seaborn has been added as a
    dependency, and you get a nicer seaborn visual?
  • *m as input: *Serge had just recommended that too, as it also gets
    around issues of weight transformations. working on this tonight for the
    next iteration.
  • *adding lines: *Also working on that, for next iteration; probably
    envelopes too? or envelopes=true as an optional input to add them?

On Sun, Apr 24, 2016 at 3:26 AM, Dani Arribas-Bel notifications@github.com
wrote:

In pysal/contrib/viz/plot.py
#797 (comment):

  • var : array
  •                  values of variable
    
  • w : array
  •                  values of spatial weight
    
  • '''
  • w.transform = 'r'
  • slag = ps.lag_spatial(w, var)
  • Z-Score standardisation

  • y_std = (var - var.mean())/var.std()
  • yl_std = (slag - slag.mean())/slag.std()
  • fig1 = plt.figure(figsize=(5,5))
  • plt.scatter(y_std, yl_std, s=60, color='k', alpha=.6)

We might want to add a linear (and potentially non-linear) fit line. Also,
maybe we could add a legend for that line that includes the coefficient for
Moran's I (as it's its slope)?

At the very least, I'd add the linear fit and maybe introduce an option
for a LOESS line, although that'd make us depend on statsmodels.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/pysal/pysal/pull/797/files/2fb3d3b7eaa9d6bb2032f7393d54f04478937eb6#r60842378

plt.axvline(0, alpha=0.5)
plt.axhline(0, alpha=0.5)

plt.show()
return None





return None