{
"cells": [
{
"cell_type": "markdown",
"id": "9dba0829-9f58-4a30-ac7b-3068db32a9c7",
"metadata": {},
"source": [
"# Other Visualizations\n",
"\n",
"`xyzpy` has two functions for visualizing arrays that are focused on quickly getting an overview of the values, and unlike plots don't require or assume any particular relationship between the dimensions. Think of these as visual pretty print functions. They are:\n",
"\n",
"- [`visualize_matrix`](xyzpy.visualize_matrix): for 2D or 1D arrays or sequences of arrays\n",
"- [`visualize_tensor`](xyzpy.visualize_tensor): for ND arrays or sequences of arrays"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "56d0f1c5-10b9-4f51-ada3-af8eccdc3740",
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"import numpy as np\n",
"\n",
"import xyzpy as xyz\n",
"\n",
"rng = np.random.default_rng(42)"
]
},
{
"cell_type": "markdown",
"id": "83c4b346-e23e-4e67-8605-ccff64644e93",
"metadata": {},
"source": [
"## Visualizing matrices\n",
"\n",
"[`visualize_matrix`](xyzpy.visualize_matrix) is for 2D or 1D arrays or sequences of arrays. It is a wrapper around [`matplotlib.pyplot.imshow`](matplotlib.pyplot.imshow)\n",
" that adds some convenient and consistent defaults and a few extra features.\n",
"\n",
"For real arrays it shows positive numbers as blue and negative numbers as orange, the maximum magnitude is displayed below the legend (colorbar). By default the alpha channel is also mapped to magnitude, so that the more transparent a pixel is, the closer to zero the value is. This can be disabled with the ``alpha_map`` argument."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8020b5c6-b9ea-4b14-a828-616749068e5f",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x = rng.normal(size=(50, 100))\n",
"xyz.visualize_matrix(x);"
]
},
{
"cell_type": "markdown",
"id": "87ea7348-b2fb-4826-a384-8c00a04fe269",
"metadata": {},
"source": [
"For complex matrices a color wheel is used, with positive imaginary values shown as green and negative imaginary values shown as pink. The magnitude is still shown below the legend, and the alpha channel is mapped to magnitude as with real matrices."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e03391f5-a3f6-4774-9b23-e8857c82f6fb",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x = rng.normal(size=(50, 30)) + 1j * rng.normal(size=(50, 30))\n",
"xyz.visualize_matrix(x);"
]
},
{
"cell_type": "markdown",
"id": "06291abe-3c55-4ad9-8940-fe9f462b692a",
"metadata": {},
"source": [
"If you supply a sequence of arrays, they will shown as a row of images, sharing the same magnitude scale and legend (colorbar). 1D arrays shown as diagonal matrices."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ca85e8eb-52c9-4b61-8ac6-5204d47b5108",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"fig, axs = xyz.visualize_matrix(\n",
" [\n",
" rng.normal(size=(10, 7)),\n",
" rng.normal(size=(7)),\n",
" rng.normal(size=(7, 11)),\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"id": "ddfde211-886f-40bc-b269-c2dfa93c09e4",
"metadata": {},
"source": [
"More examples can be found here: {ref}`ex_viz_linalg`."
]
},
{
"cell_type": "markdown",
"id": "df7eaf72-0c36-490a-809f-ba67a31e0dd5",
"metadata": {},
"source": [
"## Visualizing tensors\n",
"\n",
"[`visualize_tensor`](xyzpy.visualize_tensor) is for ND arrays or sequences of arrays. It works by assigning an angle and associated unit vector to each dimension. The values are then drawn as circles on a grid which connects all neighboring points. The color of each circle, as with [`visualize_matrix`](xyzpy.visualize_matrix), is mapped to the magnitude and sign or phase of the number."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "42667145-5228-4b05-aa3e-5b39371f7bcd",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"