0

I am trying to plot rectangles inside a circle and color each rectangle based on their value. Each rectangle depicts the failure rate at that position. Initially I have divided the range of values into 5 intervals of 20 limits each and assigned a fixed color as below example.

{'0-20':'Yellow', '21-40':'Orange', '41-60':'Coral', '61-80':'Red', '81-100':'Black'}

Later, I scrapped the idea and went with the 'plasma' cmap from matplotlib.colors.cmaps. It ideally colored the rectangle in the shades of yellow to purple. However, it misses the small data point values to show on the plot. I am looking for the flexibility of changing the range of intervals.

from matplotlib import cm
plasma = cm.get_cmap('plasma', 30)

Ideally I want something as below. If the max value of a rectangle is 92 and min value is 0. I want to divide the range into 6 intervals and plot them based on the interval . Attached is the color bar I am looking for.

Desired Colorbar

Is there a way to achieve this in matplotlib? Kindly help.

Edit: Adding few more details. I am not looking for the fixed color , rather I am looking for gradient which intensifies from lower limit to upper limit of the range in each interval.For example in the attached picture all value between 0 to 15.33 have the color intensifying from yellow to red.

triki
  • 11
  • 7
  • What you show is more a "legend" than a colorbar. Is your problem how design the corresponding colormap? Or is the problem how to get the circles to show a colorgradient? – ImportanceOfBeingErnest Dec 16 '19 at 22:34
  • Hey ImportanceOfBeingErnest, I am trying to fill the color of rectangle based on the colorbar legend I attached. The ranges of colorbar changes according to the problem. I hope I am clear now? – triki Dec 16 '19 at 23:34
  • Ideally, I am not looking for the fixed color , rather I am looking for gradient which intensifies from lower limit to upper limit of the range in each interval. – triki Dec 16 '19 at 23:36
  • 1
    Thinking more about this, I need to come to the conclusion that the legend shown is not useful at all. It is close to impossible to distinguish any values between e.g. ~20 and ~50. I would very much recommend to either use a colorbar, or to use single colors in a legend. – ImportanceOfBeingErnest Dec 16 '19 at 23:43
  • You can read up on how to customize colormaps here: https://matplotlib.org/3.1.0/tutorials/colors/colormap-manipulation.html – Joe Dec 18 '19 at 15:49

1 Answers1

0

I agree with ImportanceOfBeingErnest's comment that single colors in a legend or a regular colorbar might be helpful. Here is an example of how the former could be created using colorbar tick labelling:

# import modules
import numpy as np
import matplotlib.pyplot as plt

# select colormap
cmap = plt.cm.get_cmap('plasma')

# define bins
bins = [0, 15.333, 30.666, 46, 61.333, 76.666, 92]

# create dummy array for heatmap
imag = np.reshape(np.linspace(0, 92, 50), (10, -1))

# prepare tick positions
pairs = list(zip(bins[:-1], bins[1:]))
labs = ['']*len(bins) + ['%05.2f ≤ x < %05.2f' % pair for pair in pairs]
bins = bins + [0.5*sum(pair) for pair in pairs]
plt.imshow(imag, cmap=cmap, aspect='auto', origin='lower')

# plot colorbar
cbar = plt.colorbar(ticks=bins)
cbar.ax.set_yticklabels(labs)
plt.tight_layout()

Binned colorbar example

David
  • 1,909
  • 1
  • 20
  • 32