# \`Spy\` function behavior with sparse matrices

**URL:** <https://discourse.julialang.org/t/spy-function-behavior-with-sparse-matrices/112232>\
**Category:** Visualization\
**Tags:** plotting, sparse, sparsearrays\
**Created:** [March 28, 2024, 1:32pm UTC](https://discourse.julialang.org/t/spy-function-behavior-with-sparse-matrices/112232 "2024-03-28T13:32:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)\
**Post date:** [March 28, 2024, 1:32pm UTC](https://discourse.julialang.org/t/spy-function-behavior-with-sparse-matrices/112232/1 "2024-03-28T13:32:33Z")

</div>

I am trying to generate sparsity patterns for some matrices for a project. However, I cannot seem to find the necessary command to make all the sparsity patterns the same size. Below is an example and the output:

```julia
A = sprand(20,20,.05)
mat1 = spzeros(50,50)
mat1[31:50,31:50] = A 
mat2 = spzeros(50,50)
mat2[1:20,1:20] = A 
display(spy(mat1))
display(spy(mat2))

```

The first plot has the axes start at 31 to 50 on both dimensions, and the first is 1 to 20 and 1 to 20. However, I want _both_ to be from 1 to 50. I tried searching around for the needed option, but I could not locate any so I am making a post.

Here are screenshots of my output:

![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f0a6d6ae6b6e0143664a34e69d17b23d7116efe.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/4/2/423c5edec304e7f5aea5efab63a7643bdd1c2cba.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 28, 2024, 1:51pm UTC](https://discourse.julialang.org/t/spy-function-behavior-with-sparse-matrices/112232/2 "2024-03-28T13:51:27Z")

</div>

You just need to set the xlims and ylims. I assume this is Plots?  
Try:

```julia
A = sprand(20,20,.05)
mat1 = spzeros(50,50)
mat1[31:50,31:50] = A 
mat2 = spzeros(50,50)
mat2[1:20,1:20] = A
display(xlims!(ylims!(spy(mat1), (1,50)), (1,50)))
display(xlims!(ylims!(spy(mat2), (1,50)), (1,50)))

```

---

<div class="post-metadata">

**Author:** ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)\
**Post date:** [March 28, 2024, 1:58pm UTC](https://discourse.julialang.org/t/spy-function-behavior-with-sparse-matrices/112232/3 "2024-03-28T13:58:18Z")

</div>

Yes! Thank you.
