1

I have data indexed by two (non linear) keys. I am trying to create a scatter point of my data with each key on one axis, and a color bar representing the value of my data.

The issue with colormesh is that my data would for example look like, assuming 4 points, (1,10,1st value),(1,20,2nd value),(2,13,3rd value) and (2,14,4th value), so I can not variate each key independently. Also, there are zones with no data that should stay white.

I am new to matplotlib, and I haven't found any function that seem to be able to do this.

I assume trying to create a colorscale by hand by fixing a few value ranges and then doing a scatterplot would work, but it seems inelegant and non precise.

What would be the best approach to this issue, or is there a relevant matplotlib function/option in the library I missed ?

Xelote
  • 43
  • 3

1 Answers1

1

For simplicity I'll assume you can get a list or array of the first key and call it x, the second key and call it y, and the value and call it value.

Assuming this, you can combine seaborn and matplotlib to achieve what you're looking for:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

x, y, value = np.random.rand(3, 50)
color_map = sns.dark_palette((0.5, 0.75, 0.9), as_cmap=True)

plot_handle, axis = plt.subplots()
scatter_points = axis.scatter(x, y, c=value, cmap=color_map)
plot_handle.colorbar(scatter_points)

# possibly need plt.show() if using interactively
# or else use matplotlib save figure options.

This yields the following example plot:

example sns scatter

You can look at a wide range of color map options from what is available in sns.palettes (the ones that allow as_cmap are the easiest), including some that don't requiring configuring any color ranges, like sns.cubehelix_palette.

ely
  • 74,674
  • 34
  • 147
  • 228