ChristianM
ChristianM

Reputation: 53

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?

Upvotes: 3

Views: 4910

Answers (2)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

Or

using Plots; pgfplots()
test = randn(3,3)
heatmap(test, c = :greys)

Upvotes: 5

niczky12
niczky12

Reputation: 5073

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

Upvotes: 2

Related Questions