Skip to content Skip to sidebar Skip to footer

Plotting Dictionary Of List (topic-word Embeddings) In Python3.x

I have a dictionary called 'topic_word' topic_word = {0: [[-0.669712, 0.6868, 0.9821409999999999], [-0.925967, 0.6138399999999999, 1.247525], [-1.09941, 1.0252620000000001, 1.3278

Solution 1:

I gather from your post that you want to have normalized values for each list corresponding to a key. And, each one of these normalized lists are represented as scatter datapoints. Here's one way to do it:

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
topic_word = {0: [[-0.669712, 0.6868, 0.9821409999999999], [-0.925967, 0.6138399999999999, 1.247525], [-1.09941, 1.0252620000000001, 1.327866]], 
1: [[-0.862131, 0.890915, 1.07759], [-0.437658, 0.279271, 0.627497], [-0.437658, 0.279271, 0.627497]], 
2: [[-0.671647, 0.670583, 0.937155], [-0.675347, 0.466983, 0.8505440000000001], [-0.706244, 0.612532, 0.762877]], 
3: [[-0.8414590000000001, 0.797826, 1.124295], [-0.567535, 0.40820300000000004, 0.811368], [-0.800963, 0.699767, 0.9237989999999999]], 
4: [[-0.8560549999999999, 1.0617020000000001, 1.579302], [-0.576105, 0.5029239999999999, 0.9392], [-0.743683, 0.69884, 0.9794930000000001]]
}
colorkey={0:'red',1:'blue',2:'green',3:'black',4:'magenta'} # creating a color map for keys
for key, value in topic_word.items():
    valno=0 # keeping a count of number of lists under each topic_word (key)
    for val in value:
        meanval=np.mean(val) 
        stdval=np.std(val)
        val = (val-meanval)/(stdval) # normalized list
        ax.scatter(key*np.ones(len(val)),val,color=colorkey[key],label="Topic "+str(key) if valno == 0else"") # label is done such that duplication of legend elements is avoided
        handles, labels = ax.get_legend_handles_labels()
        valno=valno+1
fig.legend(handles, labels, loc='best')  

  

enter image description here

Post a Comment for "Plotting Dictionary Of List (topic-word Embeddings) In Python3.x"