简体   繁体   中英

Checkerboard color plot in python with well-defined co-ordinates

I have data similar in this format

f = [
  ((0, 2), 519.52876712328771), 
  ((0, 3), 514.56307692307701), 
  ((0, 4), 564.86538461538464), 
  ((1, 1), 641.70774647887322), 
  ((1, 2), 537.97177419354841), 
  ((1, 3), 529.85214285714301), 
  ((1, 4), 467.36338028169018)
]

I want to plot a checkerboard color plot of box 1 unit size. The location or the (x,y) co-ordinate will be given by f[i][0] and the color will be given by f[i][1] . As one can see the chekerboard plot will not fill up the entire image.

How would I do this in python using matplotlib?

Example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

f = [((0, 2), 519.52876712328771), ((0, 3), 514.56307692307701), ((0, 4), 564.86538461538464), ((1, 1), 641.70774647887322), ((1, 2), 537.97177419354841), ((1, 3), 529.85214285714301), ((1, 4), 467.36338028169018)]
x = []
y = []
value = ['red', 'blue', 'green', 'brown', 'black', 'aqua', 'violet']
for i in range(0, len(f)):
    j = f[i][0][0]
    k = f[i][0][1]
    x.append(j)
    y.append(k)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.axis([0, max(y), 0, max(y)])

for axis in [ax.xaxis, ax.yaxis]:
    axis.set_minor_locator(MultipleLocator(1))
    axis.set_ticks(np.arange(max(y)) + 0.5)
    axis.set_ticklabels(range(max(y)))
for i in range(0, len(f)):
    rect = plt.Rectangle((x[i],y[i]), 1, 1, color=value[i])
    ax.add_patch(rect)

ax.grid(which='minor')

plt.show()

Output:

在此处输入图片说明

Just use our own colors. Hope this helps!

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