<jupyter_start><jupyter_text>Visualising the `esda` Moran Matrix with `splot` `esda.moran.Moran_BV_matrix` offers you a tool to assess the relationship between multiple input variables and over space as bivariate and univariate Moran's I Statistics. `Moran_BV_matrix` returns a dictionary of `Moran_BV` objects which can be displayed and further analysed. In case you are not familiar with Moran Statistics, have a look at `splot`'s `esda_morans_viz.ipynb` notebook.   Contents* What to import?* Example 1: Working with arrays * Example 2: Working with a [geopandas.GeoDataFrame](http://geopandas.org/reference/geopandas.GeoDataFrame.html)  Imports<jupyter_code>from pysal.lib.weights.contiguity import Queen
from pysal.lib import examples
import pysal.lib as lp
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Example 1: Working with arrays There are generally two ways in which a `Moran_BV_matrix` and a `splot.esda.moran_facet` can be generated. The first of the two options is to use `np.arrays` representing the attributes of different variables and adding a list of variable names. This first option is a great choice in case you needed to calculate your weights separately with `pysal.lib.weights` and already have your values stored in an array. The second and more popular option is ot directly load a DataFrame. If you are unsure in how to work with `numpy` arrays or you already have your variables stored in a dataframe, we would recommend to use Example 2.In this example we will look at visualizing your results stored as a `np.array`. We know that we would like to examine all values for the variables named: `varnames = ['SIDR74',  'SIDR79',  'NWR74',  'NWR79']`. We can pass in a list of these variable names separately with `varnames=varnames`. Additionally, we need to create an `np.array` containing the values of each individual variable separately with `vars = [np.array(f.by_col[var]) for var in varnames]`:<jupyter_code>f = gpd.read_file(examples.get_path("sids2.dbf"))

varnames = ['SIDR74',  'SIDR79',  'NWR74',  'NWR79']
varnames
variable = [np.array(f[variable]) for variable in varnames]
variable[0]<jupyter_output><empty_output><jupyter_text>Next, we can open a file containing pre calculated spatial weights for "sids2.dbf". In case you don't have spatial weights, check out `pysal.lib.weights` which will provide you with many options calculating your own.<jupyter_code>w = lp.io.open(examples.get_path("sids2.gal")).read()
w<jupyter_output><empty_output><jupyter_text>Now we are ready to import and generate our `Moran_BV_matrix`:<jupyter_code>from pysal.explore.esda.moran import Moran_BV_matrix

matrix = Moran_BV_matrix(variable, w, varnames = varnames)
matrix<jupyter_output><empty_output><jupyter_text>Let's visualise our matrix with `splot.esda.moran_facet()`. You will see Univariate Moran objects with a grey background, surrounded by all possible bivariate combinations of your input dataset:<jupyter_code>from pysal.viz.splot.esda import moran_facet

moran_facet(matrix)
plt.show()<jupyter_output><empty_output><jupyter_text>Example 2: insert a DataFrame Additionally, it is possible to generte your `Moran_BV_matrix` and a `moran_facet` using a `pandas` or `geopandas` DataFrame as input. Let's have a look at a simple example examining `columbus.shp` example data:<jupyter_code>path = examples.get_path('columbus.shp')
gdf = gpd.read_file(path)<jupyter_output><empty_output><jupyter_text>In order for `moran_facet` to generate sensible results, it is recommended to extract all columns you would specifically like to analyse and generate a new GeoDataFrame:<jupyter_code>variables2 = gdf[['HOVAL', 'CRIME', 'INC', 'EW']]
variables2.head()
variables2.shape<jupyter_output><empty_output><jupyter_text>We will now generate our own spatial weights leveraging `pysal.lib` and create a second `matrix2` from our GeoDataFrame. Note that there is no list of `varnames` needed, this list will be automatically extracted from teh first row of your `gdf`:<jupyter_code>w2 = Queen.from_shapefile(path)
w2
from pysal.explore.esda.moran import Moran_BV_matrix

matrix2 = Moran_BV_matrix(variables2, w2)
matrix2<jupyter_output><empty_output><jupyter_text>Like in the first example we can now plot our data with a simple `splot` call:<jupyter_code>from pysal.viz.splot.esda import moran_facet

moran_facet(matrix2)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0802482819773886, 'fraction_numerical': 0.007980492130348038, 'mean_word_length': 6.242375601926164, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '358629', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1404, 'n_words': 517, 'path': 'notebooks/viz/splot/esda_moran_matrix_viz.ipynb', 'repo_name': 'AndrewAnnex/pysal', 'size': '4511'}