尘风
红尘往事,一切随风

思路:用open打开文件,再用a=filename.readlines()提取每行的数据作为列表的值,然后传递列表给matplotlib并引入对应库画出图像

代码实现:
import matplotlib.pyplot as plt
n=0  #定义作为后面修改列表的索引
with open(r'C:\Users\Administrator\Desktop\test.txt') as text:
    a=text.readlines()  #提取文档里面的每行作为列表的值:一行对应列表里的一个值
for i in a:
    i=i.rstrip()  #去分行符
    a[n]=int(i)  #转化为整型,本身提取出来的是字符串,传递给画图函数会得不到实际效果
    n=n+1       #每循环一次更新索引,这样就可以每次循环都更改列表里面的值
    print(i)      
print(a)
plt.plot(a)  #画图函数plt.plot(a,linewidth=5)>linewidth设置线条粗细
plt.title("cs") #设置标题,但是不支持文字
plt.xlabel("month",fontsize=14) 设置x轴标题栏 不支持文字
plt.ylabel("pay",fontsize=14) 设置y轴标题栏 不支持文字
plt.tick_params(axis='both',labelsize=14) 设置卡尺数字的大小
plt.axis([0,10,0,100])  # axis函数制定坐标轴范围,要求四个参数,前两个是X轴
plt.show()


import matplotlib.pyplot as plt

n=0
with open(r'C:\Users\Administrator\Desktop\test.txt') as text:
    a=text.readlines()
for i in a:
    i=i.rstrip()
    a[n]=int(i)
    n=n+1
    print(i)
print(a)
plt.plot(a,linewidth=5)
plt.title("cs")
plt.xlabel("month",fontsize=14)
plt.ylabel("pay",fontsize=14)
plt.tick_params(axis='both',labelsize=4)
plt.axis([0,10,0,100])
plt.show()


外部两个表分别做x,y的实现

对应代码
import matplotlib.pyplot as plt

#以外部两个txt表分别作为x,y画图
n=0
m=0
with open(r'C:\Users\Administrator\Desktop\test.txt') as text:
    a=text.readlines()
with open(r'C:\Users\Administrator\Desktop\tet.txt') as tet:
    month=tet.readlines()
for i in a:
    i=i.rstrip()
    a[n]=int(i)
    n=n+1
    print(i)
for i in month:
    i=i.rstrip()
    month[m]=int(i)
    m=m+1
print(a)
print(month)
plt.plot(month,a,linewidth=5)#自定义x,y的取值范围,以及线条粗细
plt.title("cs")
plt.xlabel("month",fontsize=14)
plt.ylabel("pay",fontsize=14)
plt.tick_params(axis='both',labelsize=10)

plt.show()

posted on 2020-09-07 23:15  一个行者  阅读(1165)  评论(0编辑  收藏  举报