简体   繁体   English

Python:在matplotlib.pyplot上绘制子列表

[英]Python: Plotting sublists on matplotlib.pyplot

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 . 子列表item one = nameX-labelitem two = scoreY 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? 我怎样才能在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()

Assuming you have a list of tuples, you can use zip to unpack X and Y elements, and then do the bar plot; 假设你有一个元组列表,你可以用zip来解包X和Y元素,然后做条形图; The tick_label can be used to label your x-axis: tick_label可用于标记x轴:

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") 

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM