|
9 | 9 | "Levi John Wolf <[email protected]>" |
10 | 10 | ) |
11 | 11 |
|
12 | | -from warnings import simplefilter, warn |
| 12 | +from warnings import simplefilter |
13 | 13 |
|
14 | 14 | import numpy as np |
15 | 15 | import pandas as pd |
|
32 | 32 | "Moran_Local_BV", |
33 | 33 | "Moran_Rate", |
34 | 34 | "Moran_Local_Rate", |
| 35 | + "plot_moran_facet", |
35 | 36 | ] |
36 | 37 |
|
37 | 38 | PERMUTATIONS = 999 |
@@ -784,6 +785,115 @@ def _Moran_BV_Matrix_array(variables, w, permutations=0, varnames=None): # noqa |
784 | 785 | return results |
785 | 786 |
|
786 | 787 |
|
| 788 | +def plot_moran_facet( |
| 789 | + moran_matrix, |
| 790 | + figsize=(16, 12), |
| 791 | + scatter_bv_kwds=None, |
| 792 | + fitline_bv_kwds=None, |
| 793 | + scatter_glob_kwds=dict(color="#737373"), |
| 794 | + fitline_glob_kwds=None, |
| 795 | +): |
| 796 | + """ |
| 797 | + Moran Facet visualization. |
| 798 | +
|
| 799 | + A matrix containing bivariate Moran plots between all pairs of variables present in |
| 800 | + the ``moran_matrix`` dictionary. On the diagonal contains global Moran plot. |
| 801 | +
|
| 802 | + Parameters |
| 803 | + ---------- |
| 804 | + moran_matrix : dict |
| 805 | + Dictionary of Moran_BV objects returned by Moran_BV_matrix |
| 806 | + figsize : tuple, optional |
| 807 | + Size of the figure. Default is (16,12) |
| 808 | + scatter_bv_kwds : keyword arguments, optional |
| 809 | + Keywords used for creating and designing the scatter points of |
| 810 | + off-diagonal Moran_BV plots. |
| 811 | + Default =None. |
| 812 | + fitline_bv_kwds : keyword arguments, optional |
| 813 | + Keywords used for creating and designing the moran fitline of |
| 814 | + off-diagonal Moran_BV plots. |
| 815 | + Default =None. |
| 816 | + scatter_glob_kwds : keyword arguments, optional |
| 817 | + Keywords used for creating and designing the scatter points of |
| 818 | + diagonal Moran plots. |
| 819 | + Default =None. |
| 820 | + fitline_glob_kwds : keyword arguments, optional |
| 821 | + Keywords used for creating and designing the moran fitline of |
| 822 | + diagonal Moran plots. |
| 823 | + Default =None. |
| 824 | +
|
| 825 | + Returns |
| 826 | + ------- |
| 827 | + ax : matplotlib Axes instance |
| 828 | + Axes in which the figure is plotted |
| 829 | + """ |
| 830 | + try: |
| 831 | + from matplotlib import pyplot as plt |
| 832 | + except ImportError as err: |
| 833 | + raise ImportError( |
| 834 | + "matplotlib must be installed to plot the simulation." |
| 835 | + ) from err |
| 836 | + |
| 837 | + nrows = int(np.sqrt(len(moran_matrix))) + 1 |
| 838 | + ncols = nrows |
| 839 | + |
| 840 | + fig, axarr = plt.subplots(nrows, ncols, figsize=figsize, sharey=True, sharex=True) |
| 841 | + fig.suptitle("Moran Facet") |
| 842 | + |
| 843 | + for row in range(nrows): |
| 844 | + for col in range(ncols): |
| 845 | + if row == col: |
| 846 | + global_m = Moran( |
| 847 | + moran_matrix[row, (row + 1) % 4].zy, |
| 848 | + moran_matrix[row, (row + 1) % 4].w, |
| 849 | + ) |
| 850 | + _scatterplot( |
| 851 | + global_m, |
| 852 | + crit_value=None, |
| 853 | + ax=axarr[row, col], |
| 854 | + scatter_kwds=scatter_glob_kwds, |
| 855 | + fitline_kwds=fitline_glob_kwds, |
| 856 | + ) |
| 857 | + axarr[row, col].set_facecolor("#d9d9d9") |
| 858 | + else: |
| 859 | + _scatterplot( |
| 860 | + moran_matrix[row, col], |
| 861 | + bivariate=True, |
| 862 | + crit_value=None, |
| 863 | + ax=axarr[row, col], |
| 864 | + scatter_kwds=scatter_bv_kwds, |
| 865 | + fitline_kwds=fitline_bv_kwds, |
| 866 | + ) |
| 867 | + |
| 868 | + axarr[row, col].spines[["left", "right", "top", "bottom"]].set_visible( |
| 869 | + False |
| 870 | + ) |
| 871 | + if row == nrows - 1: |
| 872 | + axarr[row, col].set_xlabel( |
| 873 | + str(moran_matrix[(col + 1) % 4, col].varnames["x"]).format(col) |
| 874 | + ) |
| 875 | + axarr[row, col].spines["bottom"].set_visible(True) |
| 876 | + else: |
| 877 | + axarr[row, col].set_xlabel("") |
| 878 | + |
| 879 | + if col == 0: |
| 880 | + axarr[row, col].set_ylabel( |
| 881 | + ( |
| 882 | + "Spatial Lag of " |
| 883 | + + str(moran_matrix[row, (row + 1) % 4].varnames["y"]) |
| 884 | + ).format(row) |
| 885 | + ) |
| 886 | + axarr[row, col].spines["left"].set_visible(True) |
| 887 | + else: |
| 888 | + axarr[row, col].set_ylabel("") |
| 889 | + |
| 890 | + axarr[row, col].set_title("") |
| 891 | + |
| 892 | + plt.tight_layout() |
| 893 | + |
| 894 | + return axarr |
| 895 | + |
| 896 | + |
787 | 897 | class Moran_Rate(Moran): # noqa: N801 |
788 | 898 | """ |
789 | 899 | Adjusted Moran's I Global Autocorrelation Statistic for Rate |
|
0 commit comments