1

I have written a code that sets the facecolor of a plot depending on the y axis ticks.The code that I have used is-

import matplotlib.pylab as plt
from matplotlib.font_manager import FontProperties
fig = plt.figure(figsize=(8,5)) #height x width
ax = plt.subplot(1,1,1)    
ax.spines["top"].set_visible(False)    
ax.spines["bottom"].set_visible(False)    
ax.spines["right"].set_visible(False)    
ax.spines["left"].set_visible(False)
ax.get_xaxis().tick_bottom()    
ax.get_yaxis().tick_left()

plt.ylim(0,100)
plt.yticks(range(0, 101, 10), [str(x) + "%" for x in range(0, 101, 10)], 
fontsize=5)
plt.tick_params(axis='both', which='both', bottom=False, top=False,     
labelbottom=True, left=False, right=False, labelleft=True,labelsize=5)
plt.axhspan(0, 40, facecolor=('green'))
plt.axhspan(40,60, facecolor=(255/255.,215/255.,0/255.))
plt.axhspan(60,80, facecolor=(255/255.,140/255.,0/255.))
plt.axhspan(80,100, facecolor=(139/255.,0/255.,0/255.))
plt.show()

The plot that i am getting is- enter image description here

I have used the same colors in the table for indicating the meaning of the colors in the graph. The table looks like.The code that I am using for creating the table is-

ax=plt.gca()
col_labels=['Utilization_Range','Representation','Meaning']
table_vals=[['<=40%','L','Low risk'],['41%-60%','M','Medium Risk'],['61%-80%','H','High Risk'],['>80%','^','Very High Risk']]
# the rectangle is where I want to place the table
the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*4,
                  colLabels=col_labels,
                  bbox=[0.25, 0.5, 0.5, 0.45])
for (row, col), cell in the_table.get_celld().items():
    if row==0:
        cell.set_color('blue')
    if row==1:
        cell.set_color('green')
    if row==2:
        cell.set_color((255/255.,215/255.,0/255.))
    if row==3:
        cell.set_color((255/255.,140/255.,0/255.))
    if row==4:
        cell.set_color((139/255.,0/255.,0/255.))
the_table.set_fontsize(16)
the_table.scale(2,2)

The table that I am getting is- enter image description here Now I want to set the facecolor of graph for different regions mentioned in the form of gradient of colors specified( from the gradient I mean if I am using red for y range 0 to 40 then different intensities of red should be filled in the graph from higher intensity to lower intensity towards increasing x axis). For example for range(0,40) on yaxis the color should be gradient of green color starting from darker shade to lower shade.I want to reflect same gradient then in the table also.

I have tried using "imshow" but I am not able to figure out how to incorporate that with my case.It will be great help if somebody have solved related issue.

rikki
  • 431
  • 1
  • 8
  • 18
  • Take a look https://stackoverflow.com/questions/8389636/creating-over-20-unique-legend-colors-using-matplotlib/8391452#8391452 – MikiBelavista Oct 13 '18 at 09:08
  • This is generating different color for different lines.What I want is single color with different intensity to be plotted in the same y range.-@MikiBelavista – rikki Oct 13 '18 at 09:32

0 Answers0