python基本画图
下面是自己实际遇到的画图问题,数据大家可以自己找。
- 一个图表上面画出一个图形
import matplotlib.pyplot as plt
import pandas as pd
f = open('E:\\处理资料\python\Reference_PSPF_data\PF_of_MMF1_data.csv','r')
res = pd.read_csv(f)
#由于导入的文件名含有汉字,于是必须这样去调用,不能直接使用res = pd.read_csv('文件名')
f.close()
print(res)
print(type(res))
x = res['2']
print(x)
y = res['0']
z = res['0.1']
plt.plot(x,y,label='the first line',color = 'b')
plt.plot(x,z,label='the second line',color = 'r')
plt.xlabel('y1')
plt.ylabel('y2')
plt.title('PF_of_MMF14')
plt.legend()
plt.show()
######下面的语句是ipython里面的语句
%matplotlib notebook
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("PF_of_MMF1_data.csv")
print('data')
list_1 = data['0']#此时的data['0']是表示去的data的第一列
list_2 = data['1']#此时的data['1']是表示去的data的第二列
plt.plot(list_1, list_2)
plt.ylabel('f1')
plt.xlabel('f2')
plt.show()
plt.savefig('PF_of_MMF1_z_data.png')
- 一个图标上面画出两个图形
import matplotlib.pyplot as plt
import pandas as pd
f = open('E:\\处理资料\python\Reference_PSPF_data\PF_of_MMF14_a_data.csv','r')
res = pd.read_csv(f)
f.close()
print(res)
print(type(res))
x = res['2']
#['2'],['0']注意在进行使用这个导入的数据时看这个res数据的列是如何表示的,再决定用哪一个数字
print(x)
y = res['0']
z = res['0.1']
plt.plot(x,y,label='the first line',color = 'b')
plt.plot(x,z,label='the second line',color = 'r')
plt.xlabel('y1')
plt.ylabel('y2')
plt.title('PF_of_MMF14')
plt.legend()
plt.show()
- 画出散点图(这里是两个散点图x对应y,z的散点图)
import matplotlib.pyplot as plt
import pandas as pd
f = open('E:\\处理资料\python\Reference_PSPF_data\PF_of_MMF14_a_data.csv','r')
res = pd.read_csv(f)
f.close()
print(res)
print(type(res))
x = res['2']
print(x)
y = res['0']
z = res['0.1']
plt.scatter(x,y,label='the first line', color='b', s=25, marker='.')
plt.scatter(x,z,label='the second line', color='r', s=25, marker='.')
plt.xlabel('y1')
plt.ylabel('y2')
plt.title('PF_of_MMF14')
plt.legend()
plt.show()
- 导入wine.txt数据画图
import matplotlib.pyplot as plt
import pandas as pd
f = open('wine.txt', 'r')
res = pd.read_csv(f)#此时直接用res = pd.read_csv('文件名')也可以
f.close()
print(res)
print(type(res))
x = res['14.23']
#
print(x)
y = res['1.71']
z = res['1']
plt.plot(x, z, label='the first', color='k')
plt.plot(y, z, label='the second', color='g')
plt.xlabel('f1')
plt.ylabel('f2')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
- 换一种读数据的方法画图
import matplotlib.pyplot as plt
import pandas as pd
from numpy import *
f = open('wine.txt', 'r')
res = pd.read_csv(f)
f.close()
res = array(res)#此时的array是numpy里面的,不导入就用的话就会报错
print(res)
print(type(res))#<class 'numpy.ndarray'>
x = res[:, 1]
y = res[:, 2]
z = res[:, 3]
h = res[:, -1]#取得这个数据的最后一行
print(h)
plt.plot(x, h, label='the first', color='k')
plt.plot(y, h, label='the second', color='g')
plt.plot(z, h, label='the third', color='r')
plt.xlabel('input data')
plt.ylabel('label of data')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()