Visualising entity density on a 2D plane using pcolormesh in matplotlib, Python

82 Views Asked by At

I am trying to recreate the following heatmap (created with R) with Python.

Entity density in a room. The lighter colors equate denser entity.

This heatmap represents the entity concentration in a room, where the lighter color equate denser entity. The X axis is length in meters and the Y axis is height in meters.

Currently, I have been trying to recreate the data with matplotlib's pcolormesh. Unfortunately, I cannot seem to understand how to define X and Y columns for the heatmap.

The following code produces the heatmap below:

df = pd.DataFrame({"x": [0, 0, 1,  1, 2, 2],
               "y": [3, 4, 3, 4, 3, 4],
               "concentration": [1123, 1238, 1285, 1394, 5123, 8712]})

plt.pcolormesh(df)
plt.show()

Produced heatmap by code.

Whereas I would like to see something like this: Expected result.

As you can see, for X and Y I actually use values from columns in the dataframe. Basically, X and Y are coordinates and the color at those pixels is dependent on the value of concentration (third column).

I tried to pass the arguments like that:

plt.pcolormesh([df["x"], df["z"]], df["concentration"])
plt.show()

But this leads to the following error: TypeError: pcolormesh() takes 1 or 3 positional arguments but 2 were given

How am I to represenet the concentration data as a 2D array? Am I even on the correct path?

0

There are 0 best solutions below