3

I wanted to plot list, with sublists, each containing name and score. Sublist item one = name as X-label, and item two = score as Y label.

datacount = (("mike", 9), ("john", 8), ("smith", 7), ("niki", 7), ("garlick", 7),
             ("don", 7), ("ross", 7), ("darli", 6), ("nick", 6), ("perl", 6), 
             ("cat", 5), ("dona", 4))

How can I plot this in plt?

I tried this like its dictionary, but it does not show all names and corresponding scores.

import matplotlib.pyplot as plt
datacount = D

plt.bar(range(len(D)), D.values(), align='center')
plt.xticks(range(len(D)), D.keys())

plt.show()
Psidom
  • 209,562
  • 33
  • 339
  • 356
J.A
  • 204
  • 1
  • 4
  • 13

1 Answers1

0

Assuming you have a list of tuples, you can use zip to unpack X and Y elements, and then do the bar plot; The tick_label can be used to label your x-axis:

import matplotlib.pyplot as plt 
%matplotlib inline

plt.figure(figsize = (10, 7))

X, Y = zip(*datacount)
ax = plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X, color="sienna") 

enter image description here

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thank you, it works, how do I remove some of my X which equals to [('', 3467), ('\n', 31) ? Y=3467 totally destroys the figure. I can't use the .remove() method, right? – J.A Mar 08 '17 at 15:22
  • You can filter your `datacount` use a list comprehension. `datacount = [x for x in datacount if x[0] not in ['', '\n']]` – Psidom Mar 08 '17 at 15:25
  • And if the names are too long, and horizontal names are running into each other, how can I increase the horizontal scale, so names don't run into each other? – J.A Mar 08 '17 at 15:33
  • You can rotate the x axis labels with `ax = plt.xticks(rotation=70)` replace `70` with any angle as you see fit. – Psidom Mar 08 '17 at 15:35
  • Thank you, rotation is nice! and list comprehension is very handy, I can set min/max Y values. Why does plt.savefig("ABC.pdf") not work? – J.A Mar 08 '17 at 15:46
  • If some item names are like "- / digits" such as "- / 210505050", "- / 110505050", "- / 310505050", how do I remove them? datacount = [x for x in datacount if x[0] not in ['- / 2']] does not help. – J.A Mar 08 '17 at 16:38
  • That has been out of the range of this question which from the OP is about plotting. I recommend you first do some research on google and if you can't find the answer, ask another question separately. I believe there will be good answers for this question. But most likely you need regex for that problem or `str.startswith` method. – Psidom Mar 08 '17 at 16:41
  • Okay noted I tried the str.startwith() on this: ('- / 229613_at', 12), datacount = [x for x in datacount if x[0] not in ['str.startwith(- / 2) == True']] But this ('- / 229613_at', 12) still showed up in results. I am new python learner, thank you for encouragement, I remember doing this stuff in excel, took really long time to sort stuff. – J.A Mar 08 '17 at 17:00
  • Try `[x for x in datacount if not x[0].startswith("- / ")]` – Psidom Mar 08 '17 at 17:02