fosho
fosho

Reputation: 1676

Graphing a colored grid in python

I am trying to create a 2D plot in python where the horizontal axis is split into a number of intervals or columns and the color of each column varies along the vertical axis.

The color of each interval depends on the value of a periodic function of time. For simplicity, let's say these values range between 0 and 1. Then values closer to 1 should be dark red and values close to 0 should be dark blue for example (the actual colors don't really matter).

Here is an example of what the plot should look like:

Example of plot

Is there a way to do this in Python using matplotlib?

Upvotes: 1

Views: 3383

Answers (1)

Suever
Suever

Reputation: 65430

This is really just displaying an image. You can do this with imshow.

import matplotlib.pyplot as plt

import numpy as np

# Just some example data (random)
data = np.random.rand(10,5)

rows,cols = data.shape

plt.imshow(data, interpolation='nearest', 
                 extent=[0.5, 0.5+cols, 0.5, 0.5+rows],
                 cmap='bwr')

enter image description here

Upvotes: 4

Related Questions