简体   繁体   English

如何使用Matplotlib pyplot pcolor为绘图中的每个网格提供不同的颜色

[英]How do I use Matplotlib pyplot pcolor to provide distinct color to each grid in my plot

I have built some fairly simple logarithmic matplotlib scatter plots. 我建立了一些相当简单的对数matplotlib散点图。 I am happy with the results thus far, but I need to change the color of each grid area. 到目前为止,我对结果感到满意,但需要更改每个网格区域的颜色。

Current Plot: 当前图:

在此处输入图片说明

I would like to understand how I could do the following: 我想了解如何执行以下操作:

在此处输入图片说明

I appreciate any help. 感谢您的帮助。 Thanks. 谢谢。

Plot a small image underneath the scatter plot, with an integer indicating the tile color. 在散点图下方绘制一个小图像,其中一个整数指示图块颜色。 You can then use pcolor to plot the image, with edgecolors to define the borders. 然后,您可以使用pcolor绘制图像,并使用edgecolors定义边界。 The code below does this, with cell color defined as the maximum of cell index i, j, which happens to match your grid. 下面的代码执行此操作,将单元格颜色定义为单元格索引i,j的最大值,恰好与您的网格匹配。

import numpy as np
import matplotlib.pyplot as plt

# define grid
nx, ny = 6, 5
x, y = np.arange(nx), np.arange(ny)
xx, yy = np.meshgrid(x, y)
z = np.maximum(xx, yy)

# create random points
npoints = 30
data_x = 10**(np.random.rand(npoints)*nx)
data_y = 10**(np.random.rand(npoints)*ny-1)

# plot grid then points
plt.pcolor(10.**x, 10.**(y-1), z, edgecolors='k')
plt.loglog(data_x, data_y, '.w')
plt.axis([1,10**5,0.1,10**3])
plt.show()

Note that you could also use zorder=n to force the scatter plot above the image. 请注意,您还可以使用zorder = n强制将散点图置于图像上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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