Skip to content

Assigning individual colors to hexagons

alexkaz2 edited this page Nov 4, 2021 · 6 revisions
  1. Create a hexagonal grid (make sure it is large enough to allow sufficient resolution):
from hexalattice.hexalattice import *
hex_centers, _ = create_hex_grid(nx=50,
                                 ny=50,
                                 do_plot=False)
x_hex_coords = hex_centers[:, 0]
y_hex_coords = hex_centers[:, 1]
  1. Generate a color array that specifies colors of every hexagon, with the following format:

    1. Type = nd.array (e.g. created by colors = np.zeros([50, 3]))
    2. Number of rows equals to number of hexagons
    3. Each row represent valid RGB triplet (e.g. [0.3, 0.1, 1]).

    Or sample colors from an image by:

image_path = 'example_image.jpg'     # Works with .png, .jpg, .tif
colors = sample_colors_from_image_by_grid(image_path, x_hex_coords, y_hex_coords)
  1. Plot the hexagonal lattice with custom colors:
plot_single_lattice_custom_colors(x_hex_coords, y_hex_coords,
                                      face_color=colors,
                                      edge_color=colors,
                                      min_diam=0.9,
                                      plotting_gap=0.05,
                                      rotate_deg=0)
plt.show()

The result:

  1. See further customization of the coloring module in the 5th example of the main() method, in hexalattice.py: To run the example, open python terminal in the folder with hexalattice.py, and execute: python hexalattice.py

The fifth image is expected to be:

The code that generates it is:

plot_single_lattice_custom_colors(hex_centers[:, 0], hex_centers[:, 1],
                                  face_color=colors,
                                  edge_color=colors,
                                  min_diam=1.,
                                  plotting_gap=0,
                                  rotate_deg=0,
                                  line_width=0.3,
                                  h_ax=axs[0, 1])

plot_single_lattice_custom_colors(hex_centers[:, 0], hex_centers[:, 1],
                                  face_color=colors,
                                  edge_color=colors*0,
                                  min_diam=1.,
                                  plotting_gap=0,
                                  rotate_deg=0,
                                  line_width=1.,
                                  h_ax=axs[1, 0])

plot_single_lattice_custom_colors(hex_centers[:, 0], hex_centers[:, 1],
                                  face_color=colors,
                                  edge_color=colors*0,
                                  min_diam=1.,
                                  plotting_gap=0,
                                  rotate_deg=0,
                                  line_width=0.1,
                                  h_ax=axs[1, 1])
'''
Clone this wiki locally