Skip to content

Commit c6bae7d

Browse files
committed
Python-Notes-33924
1 parent 3e4f056 commit c6bae7d

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Matplotlib条形图
2+
3+
这个教程中我们会涉及条形图和直方图。我们先来看条形图:
4+
5+
函数是`plt.bar`,提示一下,`import matplotlib.pyplot as plt`这一句和`from matplotlib import pyplot as plt`是一样的,使用函数时两个列表是两组坐标,第一个列表是x轴,第二个是y轴
6+
7+
```python
8+
import matplotlib.pyplot as plt
9+
plt.bar([1,3,5,7,9],[5,2,7,8,2], label="Example one")
10+
plt.bar([2,4,6,8,10],[8,6,2,5,6], label="Example two", color='g')
11+
plt.legend()
12+
plt.xlabel('bar number')
13+
plt.ylabel('bar height')
14+
plt.title('Epic Graph\nAnother Line! Whoa')
15+
plt.show()
16+
```
17+
18+
`plt.bar` 为我们创建条形图。 如果你没有明确选择一种颜色,那么虽然做了多个图,所有的条看起来会一样。 这让我们有机会使用一个新的 Matplotlib 自定义选项。 你可以在任何类型的绘图中使用颜色,例如 `g` 为绿色, `b` 为蓝色, `r` 为红色,等等。 你还可以使用十六进制颜色代码,如 `#191970`
19+
20+
![image-20200702122952092](E:\ProgramThomas\Coding-Notes\Python-Notes\例程\##matplotlib\PDF教程\DOC\image-20200702122952092.png)
21+
22+
# Matplotlib直方图
23+
24+
接下来,我们会讲解直方图。 直方图非常像条形图,倾向于通过将区段组合在一起来显示分布。 这个例子可能是年龄的分组,或测试的分数。 我们并不是显示每一组的年龄,而是按照 20 ~ 25,25 ~ 30... 等等来显示年龄。 这里有一个例子:
25+
26+
```python
27+
import matplotlib.pyplot as plt
28+
population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,1
29+
21,122,130,111,115,112,80,75,65,54,44,43,42,48]
30+
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
31+
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
32+
plt.xlabel('x')
33+
plt.ylabel('y')
34+
plt.title('Interesting Graph\nCheck it out')
35+
plt.legend()
36+
plt.show()
37+
```
38+
39+
对于 plt.hist ,你首先需要放入所有的值,然后指定放入哪个桶或容器。 在我们的例子中,我们绘制了一堆年龄,并希望以 10 年的增量来显示它们。 我们将条形的宽度设为 0.8,但是如果你想让条形变宽,或者变窄,你可以选择其他的宽度。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import matplotlib.pyplot as plt
2+
plt.bar([1, 3, 5, 7, 9], [5, 2, 7, 8, 2], label="Example one")
3+
plt.bar([2, 4, 6, 8, 10], [8, 6, 2, 5, 6], label="Example two", color='g')
4+
plt.legend()
5+
plt.xlabel('bar number')
6+
plt.ylabel('bar height')
7+
plt.title('Epic Graph\nAnother Line! Whoa')
8+
plt.show()
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from tkinter import *
2+
def calculate():
3+
result=eval(equ.get())
4+
equ.set(equ.get()+"=\n"+str(result))
5+
def show(buttonString):
6+
content=equ.get()
7+
if content=="0":
8+
content=""
9+
equ.set(content+buttonString)
10+
root = Tk()
11+
root.title("计算器")
12+
equ=StringVar()
13+
equ.set("0")
14+
label=Label(root, width=25, height=2, relief=RAISED, anchor=SE,
15+
textvariable=equ)
16+
label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
17+
clearButton=Button(root, text="C",fg="blue",width=5,command=lambda:equ.set("0"))
18+
clearButton.grid(row=1, column=0)
19+
Button(root, text="DEL",width=5,command=lambda:equ.set(str(equ.get()[:-1]))).grid(row=1,column=1)
20+
Button(root, text="%", width=5, command=lambda:show("%")).grid(row=1,column=2)
21+
Button(root, text="/", width=5, command=lambda:show("/")).grid(row=1,column=3)
22+
Button(root, text="7", width=5, command=lambda:show("7")).grid(row=2,column=0)
23+
Button(root, text="8", width=5, command=lambda:show("8")).grid(row=2,column=1)
24+
Button(root, text="9", width=5, command=lambda:show("9")).grid(row=2,column=2)
25+
Button(root, text="*", width=5, command=lambda:show("*")).grid(row=2,column=3)
26+
Button(root, text="4", width=5, command=lambda:show("4")).grid(row=3,column=0)
27+
Button(root, text="5", width=5, command=lambda:show("5")).grid(row=3,column=1)
28+
Button(root, text="6", width=5, command=lambda:show("6")).grid(row=3,column=2)
29+
Button(root, text="-", width=5, command=lambda:show("-")).grid(row=3,column=3)
30+
Button(root, text="1", width=5, command=lambda:show("1")).grid(row=4,column=0)
31+
Button(root, text="2", width=5, command=lambda:show("2")).grid(row=4,column=1)
32+
Button(root, text="3", width=5, command=lambda:show("3")).grid(row=4,column=2)
33+
Button(root, text="+", width=5, command=lambda:show("+")).grid(row=4, column=3)
34+
Button(root, text="0", width=5, command=lambda:show("0")).grid(row=5,column=0,columnspan=2)
35+
Button(root, text=".", width=5, command=lambda:show(".")).grid(row=5,column=2)
36+
Button(root, text="=", width=5, command=lambda:calculate()).grid(row=5,column=3)
37+
root.mainloop()

0 commit comments

Comments
 (0)