python的pandas使用(二)

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,100)
y = np.sin(x)


#默認繪製樣式,plot第三個參數可以修改繪製的樣式,第四個參數控制顏色color
#plt.plot(x,y)
plt.plot(x,y,'--',color='red')
plt.plot(x,np.cos(x))

在這裏插入圖片描述

#生產圖片
fig = plt.figure()
plt.plot(x,y,'.')
fig.savefig('1.png');
#在不同的圖片(2行1列)上繪製曲線
plt.subplot(2,1,1)
plt.plot(x,np.sin(x),'--')

plt.subplot(2,1,2)
plt.plot(x,np.cos(x),'--')

在這裏插入圖片描述

#添加圖例
plt.plot(x,y,'--',label='sin(x)')
plt.plot(x,np.cos(x),label='cos(x)')
plt.legend()

在這裏插入圖片描述

#設置圖例的位置
plt.plot(x,y,'--',label='sin(x)')
plt.plot(x,np.cos(x),label='cos(x)')
plt.legend(loc="upper right")

在這裏插入圖片描述

#plot定製的參數非常多
x = np.linspace(0,10,20)
y = np.sin(x)
plt.plot(x,y,'-p',color='orange',markersize=16,linewidth=4,markeredgecolor='gray',markeredgewidth=1)
#限制y軸的取值範圍
plt.ylim(-0.5,1.2)

在這裏插入圖片描述

#繪製散點圖
x = np.linspace(0,10,20)
y = np.sin(x)
plt.scatter(x,y,s=20,c='blue')

在這裏插入圖片描述

#隨機生產散點圖
#設置樣式-經典樣式
plt.style.use('classic')

x = np.random.random(100)
y = np.random.random(100)
colors = np.random.rand(100)
sizes = 1000 * np.random.random(100)
plt.scatter(x,y,c = colors ,s=sizes,alpha=0.4)
#繪製顏色條
plt.colorbar()

在這裏插入圖片描述

import pandas as pd

#pandas本身自帶繪圖


#線性圖
data = pd.DataFrame(np.random.rand(100,4).cumsum(0),columns=['A','B','C','D'])
data.plot()

#柱狀圖
data = pd.DataFrame(np.random.rand(100,4).cumsum(0),columns=['A','B','C','D'])
#data.plot.bar()
#等同於
data.plot(kind='bar')

#直方圖
data = pd.DataFrame(np.random.rand(100,4),columns=['A','B','C','D'])
data.hist()


plt.show()

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

aaa

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章