Last year I posted a question about making graphical representations of sentences, and at the end I did it with matplotlib. Now I wanted to add some more features to it, but I am stuck.
you can see the previous post from this link

Now, if we assign a number to each letter in a sentence and get a list of these numbers,
can I create a 1D heat map with this list as and additional bar?

First problem will be to get the range of numbers and normalize it, or we should be able to assign a range for the heat map.

It would be great if you can help.

here is an example code from the previous post.

text ='''World number two Nadal breezed through the first set, taking it with breaks of serve in the fourth and sixth games.
Monfils put up more resistance in an erratic second set but his wayward display was summed up by a double fault which sealed Nadal's victory.
The Spaniard will face a semi-final against compatriot Nicolas Almagro, who beat Austrian Juergen Melzer 6-3 6-1.
In the women's competition, fourth seed Venus Williams booked her place in the semi-finals with a 6-3 6-3 win over Australia's Samantha Stosur.'''
import pylab
import matplotlib

# Put text into list of sentences            
sentences = []
sentences = text.split('\n')

# Get the lenght of longest sentence-Lmax and number of sentences
it = 0
for i in sentences:
      it = it + 1
      LenSen= len(i)
      if LenSen > Lmax:
            Lmax = LenSen
totalnumber = it

#Set the image properties
pylab.rcParams['figure.figsize']       = [Lmax/20, totalnumber]
pylab.rcParams['figure.subplot.left']  = 0.2 # Left margin
pylab.rcParams['figure.subplot.right'] = 1.0-0.04 # Right margin
pylab.rcParams['font.size'] = 7

# Axes range from 0 to 1 for x-axis (simplifying calls to .axhspan()) and 
# 20 down to 0 for Y-values because that's convenient for this example
ax = [0, 1, totalnumber+1, 0] # [xlo, xhi,   ylo, yhi]

# printer is a drawing module
def printer(searchterm, Input,start,end,xstart,color):
      lens = len(searchterm)
      while True:
             inDex = Input.find(searchterm, start,end)
             if inDex == -1:
             inDex = float(inDex)
             matplotlib.pyplot.axhspan(it+xstart,it+0.2+xstart, xmin=inDS, xmax=inDE, ec='k', fc=color)
             # if search string not found, find() returns -1
             # search is complete, break out of the while loop

             # move to next possible start position
             inDex= int(inDex)
             start = inDex + 1
for i in sentences:
      it = it+1
      LenSen= len(i)
      LenF = float(LenSen)
      matplotlib.pyplot.axhspan(it+0.2,it+0.4, xmin=0, xmax=LenF/Lmax, ec='k', fc='r') #creates sentence bar
      pylab.text(-0.2, it+0.4, 'sentence'+str(it)+' with spaces') # create the sentence bar 
      printer(' ', i, 1, LenSen,0.2,'b')    #blue represents the spaces on sentence bar
      printer('the', i, 1, LenSen,0,'y') #above sentence bar, yellow represents 'the'
      pylab.text(-0.2, it+0.2, 'the')


matplotlib.pyplot.xticks((),()) # No labels for X-axis
matplotlib.pyplot.yticks((),()) # No labels for Y-axis

Recommended Answers

All 3 Replies

Can you be more specific about exactly what you want to plot. You have a list of numbers. You wanted to do some operations, like normalization and stuff... after you did all of these, what would you be left with? Strictly an array of numbers still?

The idea is to have an heat map under the bars created by the code I posted. I attached an example image, the heat map is copy paste.
I can create the graphical representation without the heat map, and also the array of the numbers. I just need to put these numbers in the form of a heat map.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.