Python Forum
How to plot legend for a colormap?
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to plot legend for a colormap?
#2
Hi all,

I wrote a more complete function which works well too, but I still have no idea, about how to display a color bar based on the colors that fills the rectangles. Here's the updated code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import cm 
from colour import Color

def tabularS(X,dx,p,q,prism):
   vm = 3*max(p[:,3])
   unit = dx/len(X) # half size for minimun value of MDT
   lenp=[]

   cont1 = 0

# Ordenating the p vector
   posit = np.argsort(p[:,0])
   p = p[posit,:]
   norm = plt.Normalize()
   colors = plt.cm.rainbow(norm(p[:,0]))

   ax3 = fig.add_subplot(313)

# Rectangle width
   for i in range(1,prism):
       aux = p[i,0]/p[0,0]
       lenp.append(unit*aux)

# Plot and fill
   for i in range(prism):
       xl = (p[i,2]-unit)

       cor = colors[cont1]
       cont1+=1
       rect = Rectangle((xl,p[i,3]),unit,vm-p[i,3],color=cor)
       ax3.add_patch(rect)

       plt.xlim(X[0], X[-1])
       plt.ylim([0, vm])
       plt.gca().invert_yaxis()
       plt.title('Position')


       if p[i,1] <= 0:
           rect = Rectangle((xl,p[i,3]),unit,vm-p[i,3],linestyle = '-',fill = False, hatch = '//')
           ax3.add_patch(rect)

           plt.xlim(X[0], X[-1])
           plt.ylim([0, vm])
           plt.gca().invert_yaxis()
           plt.title('Position')

       rect = Rectangle((xl,q[i,3]), unit, vm-q[i,3], linestyle = '--', fill = False)
       ax3.add_patch(rect)

       plt.xlim(X[0], X[-1])
       plt.ylim([0, vm])
       plt.gca().invert_yaxis()
       #plt.colorbar(colors,ax3)
       plt.title('Prisms Position')
       plt.ylabel('Depth (m)')
       plt.xlabel('Distance (m)')

   plt.show()
   return
If it is possible to help me with this problem, I will be very grateful.
Reply


Messages In This Thread
How to plot legend for a colormap? - by Felipe - Jun-02-2017, 10:00 PM
RE: How to plot legend for a colormap? - by Felipe - Jun-05-2017, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Enhance my stacked barplot title, and flip legend Clunk_Head 0 1,675 Jul-08-2019, 03:30 AM
Last Post: Clunk_Head
  Colormap data from file - pcolormesh problem alrm31015 0 2,539 May-19-2019, 10:20 PM
Last Post: alrm31015

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020