python - Scatter Plot dictionary of dictionary -
i need scatter plot dictionary of dictionary.
my data looks :
{'+c': {1: 191, 2: 557}, '+b': none, '-b': none, '+d': none, '+n': {1: 1, 3: 1}, '+l': {1: 2819, 2: 1506}, '>l': none, '<c': {0: 2125}, '<b': none, '<l': {0: 2949, 1: 2062}}
the outer keys x axis labels , inside keys y axis. inside keys' values annotations x,y. tried plotting data didn't graph looking for.
i tried following ended having repetitions in x axis.
for action, value in action_sequence.items(): if value: seq,count in value.items(): data["x"].append(action) data["y"].append(seq) data["label"].append(count) else: data["x"].append(action) data["y"].append(-1) data["label"].append(0) print(data) plt.figure(figsize=(10,8)) plt.title('scatter plot', fontsize=20) plt.xlabel('x', fontsize=15) plt.ylabel('y', fontsize=15) plt.xticks(range(len(data["x"])), data["x"]) plt.scatter(range(len(data["x"])), data["y"], marker = 'o')
you need set integer categories x can assign labels on x axis. code below uses action codes in dict this. note dictionaries in python unordered, keys need sorted. instead use ordered dict, , use dict.keys() directly.
the annotations each point strings placed 1 @ time text annotations on plot. need explicitly set x,y axis ranges on plot plt.axis() since annotations aren't included when computing ranges automatically.
import matplotlib.pyplot plt action_sequence = { '+c': {1: 191, 2: 557}, '+b': none, '-b': none, '+d': none, '+n': {1: 1, 3: 1}, '+l': {1: 2819, 2: 1506}, '>l': none, '<c': {0: 2125}, '<b': none, '<l': {0: 2949, 1: 2062} } # x data categorical; define lookup table mapping category string int x_labels = list(sorted(action_sequence.keys())) x_values = list(range(len(x_labels))) lookup_table = dict((v,k) k,v in enumerate(x_labels)) # build list of points (x, y, annotation) defining scatter plot. points = [(lookup_table[action], key, anno) action, values in action_sequence.items() key, anno in (values if values else {}).items()] x, y, anno = zip(*points) # y categorical, integer labels categories y_values = list(range(min(y), max(y)+1)) y_labels = [str(v) v in y_values] plt.figure(figsize=(10,8)) plt.title('scatter plot', fontsize=20) plt.xlabel('x', fontsize=15) plt.ylabel('y', fontsize=15) plt.xticks(x_values, x_labels) plt.yticks(y_values, y_labels) plt.axis([min(x_values)-0.5, max(x_values)+0.5, min(y_values)-0.5, max(y_values)+0.5]) #plt.scatter(x, y, marker = 'o') x_k, y_k, anno_k in points: plt.text(x_k, y_k, str(anno_k)) plt.show()
see following question different way of labeling scatter plot:
Comments
Post a Comment