数据可视化
1
列表[];元组()
a=[1,4,6,8,5]
a.append(1)
a=[1,4,6,8,5,1]
for index,item in a:
print(index,item)
2
标签、坐标
matplotlib
from pylab import plot,show
plot(x,y)
show()
plot(x,y,marker='o')
plot(y,marker='o')
x=range(12)
plot(x,y1,y2)
from pylab import legend()
legend([label 1,label])
from pylab import title,xlabel,ylabel
3
非交互
'''
simple plot using pyplot
'''
import matplotlib.pyplot
def:
x=[1,2,3,4]
y=[2,4,6]
matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()
'''
abbre
'''
import matplotlib.pyplot as plt
def plot1():
x=[1,2,3,]
y=[2,4,6]
plt.plot(x,y)
plt.show()
4设置坐标轴
from pylab import axis
axis(ymin=0)
axis(0,20,0,20)
5保存
import matplotlib.pyplot as plt
x=[1,2,3]
y=[4,5,9]
plt.plot(x,y,marker='o')
plt.show()
plt.savefig('C:\\graph.png')
6练习
1)反比例函数
import matplotlib.pyplot as plt
x=range(1,13)
a=[]
for i in x:
y=1.0/i
a.append(y)
# print(a)
plt.plot(x,a,marker='o')
plt.title('inverse function')
plt.show()
plt.savefig('F:\\python_program\\savefig_inverse')
2)抛物线运动
from matplotlib import pyplot as plt
import math
g= 9.8
u=[]
theta=[]
def draw(x,y):
plt.plot(x,y,marker='o')
plt.title('motion')
plt.xlabel('x')
plt.ylabel('y')
def point(u, theta,time):
x=[]
y=[]
t=0.0
while (t<time):
x.append(u*math.cos(theta)*t)
y.append(u*math.sin(theta)*t-0.5*g*t**2)
t=t+0.001
draw(x,y)
try:
while(1):
c=float(input("please enter u :"))
u.append(c)
if ' '==input():
break
while(1):
d=float(input("please enter theta:"))
d=math.radians(d)
theta.append(d)
if ' '==input():
break
except ValueError:
print('invalid input')
else:
#print(u)
#print(theta)
amount=len(u)
for i in range(amount):
flight_time= u[i]*math.sin(theta[i])*2/g
point(u[i],theta[i],flight_time)
plt.show()