2015-09-06 119 views
1

我是Python编程新手。我正在学习Python。在python中绘制条形图

下面的代码帮我画了一张条形图。我想了解代码。 我看不惯线5,6,7和8即

N = len(data) 
x = np.arange(1,N+1) 
y = [num for (s, num) in data ] 
labels = [ s for (s, num) in data ] 

而且,我们为什么要采取x+width/2.0,同时绘制x轴的标签? 而且,如何在房子盗窃之前在图表的开始处添加一个小宽度?酒吧通常以0开头。我不知道如何在第一个酒吧开始前带上一个小宽度。我试过了,但没有好转。

完整的程序如下。

import matplotlib.pyplot as plt 
import numpy as np 
data = [ ("House Theft", 57), ("House Fire", 48), 
      ("Car Theft", 156), ("Car Accident", 245)] 
N = len(data) 
x = np.arange(1,N+1) 
y = [num for (s, num) in data ] 
labels = [ s for (s, num) in data ] 
width = 0.35 #Use 1 to make it as a histogram 
bar1 = plt.bar(x, y, width, color="y") 
plt.ylabel('Frequency') 
plt.xticks(x + width/2.0, labels) 
plt.show() 
+0

'''data'''是一个列表 - ['''len'''](https://docs.python.org/3/library/functions.html#len)。 [''numpy'''](http://docs.scipy.org/doc/numpy/user/)('''np''')是一个库 - ['''np.arange''' ](http://docs.scipy.org/doc/numpy-1.6.0/reference/generated/numpy.arange.html)。第7行和第8行是[list comprehensions](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)。 – wwii

+0

@wwii这行'num(s,num)in data'表示什么?我了解数据是范围。这些变量和数字表明什么?与字符串和数字有关?他们究竟想要在这里做什么? –

+0

['''tuple''' unpacking](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)。解释型语言的一个好处是,您可以轻松地使用shell中的语言功能来尝试一些东西,看看它是如何工作的。 Python有一个相当不错的[教程](https://docs.python.org/3/tutorial/index.html)。 – wwii

回答

1

Multiple assignment, tuple/sequence packing/unpacking

>>> 
>>> data = [ ("House Theft", 57), ("House Fire", 48), 
      ("Car Theft", 156), ("Car Accident", 245)] 
>>> 
>>> for thing in data: 
    (s, num) = thing 
    print thing, '\t', s, '\t', num 

('House Theft', 57)  House Theft  57 
('House Fire', 48)  House Fire  48 
('Car Theft', 156)  Car Theft  156 
('Car Accident', 245) Car Accident 245 
>>> 

>>> for (s, num) in data: 
    print s, '\t\t', num 


House Theft   57 
House Fire   48 
Car Theft   156 
Car Accident  245 
>>> 

plt.xticks(x + width/2.0, labels)将由上的宽度的一半的偏移在x轴上的刻度。不知道为什么这样做,除了可能的视觉效果。

>>> x = np.arange(1,11) 
>>> x 
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 
>>> width = .5 
>>> x + width/2 
array([ 1.25, 2.25, 3.25, 4.25, 5.25, 6.25, 7.25, 8.25, 9.25, 10.25]) 
>>> 
+1

我现在试过了。它帮助我很清楚地理解它。这种语言非常好。我曾在C,C++和Java中工作。相比之下,这里的一切都更简单。非常感谢! –

0
  • N = len(data)

N的值现在是在阵列data的长度。在你的情况下,data的长度是4

  • x = np.arange(1,N+1)

x的值现在是[1, 2, 3, 4],看到从doc这个例子:

>>> np.arange(3) 
array([0, 1, 2]) 
>>> np.arange(3.0) 
array([ 0., 1., 2.]) 
>>> np.arange(3,7) 
array([3, 4, 5, 6]) 
>>> np.arange(3,7,2) 
array([3, 5]) 
  • y = [num for (s, num) in data ]

y的值是[57, 48, 156, 245]

for (s, num) in data重复data的值。 由于data的值有两个部分("House Theft", 57),对于每个循环,s取第一部分(第一个循环的"House Theft")的值和第二个部分的值(第一个循环的57)。既然你只需要数字(第二部分),num for (s, num) in data只需要num,然后你的数组被填充,因为表达式在括号内[]之间。

它从表达式num for (s, num) in data的“结果”中创建一个数组。

  • labels = [ s for (s, num) in data ]

和以前一样,但与字符串,而不是价值。

我仍然对命名法(数组,元组,列表...)有困惑,如果有人可以检查我的答案,我会很感激它,因为它会帮助作者和我学习正确的Python词汇表!

+0

非常感谢您的清楚解释。 –