简体   繁体   中英

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:

情节的例子

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

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')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM