4
$\begingroup$

I write following program in Python to plot from array:

import matplotlib.pyplot as pyplot
import numpy

p = 5
n = 25
s = numpy.zeros([n,n])

s[0, 0] = 1
for i in range(0, n):
    for j in range(0, i+1):
        s[i, j] = i+j

pyplot.imshow(s)
pyplot.show()

to get following image.

image

I would like to imitate such plotting style of image (grid type and each mesh has color corresponds to its array value) in Mathematica, too. How to implement this in Mathematica? I found ListDensityPlot, but it seems not what I want. Anyway, if such function exists, I hope its input is 2D-List(or array).

Apologize that my question is not detailed, since I don't know much about Mathematica. If it's duplicate, please let me know.

$\endgroup$
  • 3
    $\begingroup$ How about ArrayPlot and MatrixPlot? $\endgroup$ – Henrik Schumacher Apr 8 at 6:09
  • $\begingroup$ @HenrikSchumacher Thanks a lot that's exactly what I want. $\endgroup$ – ChoMedit Apr 8 at 6:14
  • $\begingroup$ You're welcome! $\endgroup$ – Henrik Schumacher Apr 8 at 6:21
9
$\begingroup$

There are a few ways to get what you want. One way to get close would be:

ArrayPlot[
  PadRight[Table[i + j, {i, n}, {j, i}], {n, n}],
  ColorFunction -> "BlueGreenYellow",
  FrameStyle -> Black,
  FrameTicks -> {{True, False}, {True, False}},
  PlotRangePadding -> 0
]

ArrayPlot of poster's data.

If you delve deeper into the various options, it's not too difficult to replicate exactly the figure you show. One thing to note is that Mathematica indexes arrays/lists/vectors/matrices from 1 rather than 0. If you want to start from 0, you'll need to take a look at the DataRange option, or else specify the coordinates as a list {{0, 0, 1}, {0, 1, 1}, ...} etc.

The other thing I would suggest when learning Mathematica: get used to the documentation system. It's extraordinarily advanced compared to any other language I've learned. It starts with very simple, self-contained examples and gets more and more complicated. You can also change the code inside the documentation system and run it directly to see what happens.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.