Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Plot a matrix as an image using pgfplots package

From my calculations I obtain a 2D array of real numbers. What i want to do with them is to plot them as an image where the values of the array's elements translate into a colormap. Till now I used the package PyPlot for this kind of visualization. With Pyplot it was pretty easy. An example using gray values would be

using PyPlot

test = rand(3,3);

PyPlot.gray()
imshow(test,interpolation="none")
colorbar()

Is there a way to do the same but with the PGFPlots package instead of PyPlot? I have already tried to use Plots.Image but that did not work with an array instead of a function. Any ideas?

like image 356
ChristianM Avatar asked Sep 06 '26 13:09

ChristianM


2 Answers

Or

using Plots; pgfplots()
test = randn(3,3)
heatmap(test, c = :greys)
like image 51
Michael K. Borregaard Avatar answered Sep 08 '26 01:09

Michael K. Borregaard


Not sure if there's a function for this in PGFPlots but you can hack you way around it, by creating a new function:

function plot_matrix(my_matrix)
    n,m = collect(size(my_matrix)) .+ 1 .- 1e-10
    f(x,y) = my_matrix[Int(floor(x)), Int(floor(y))]
    Plots.Image(f, (1, m), (1, n))
 end

This gives you the following results:

test = rand(3,3);
plot_matrix(test)

enter image description here

test2 = rand(15, 15);
plot_matrix(test2)

enter image description here

like image 25
niczky12 Avatar answered Sep 08 '26 02:09

niczky12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!