散点图:
import numpy as np
import matplotlib.pyplot as plt
open,high=np.loadtxt('000001.csv',delimiter=',',skiprows=1,usecols=(1,2),unpack=True)
change=high-open
yesterday=change[:-1]
today=change[1:]
plt.scatter(today,yesterday)
折线图:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x=np.linspace(0,10,100)
y=np.sin(x)
plt.plot(x,y)
plt.show()
date,open,high,low,close=np.loadtxt('000001.csv',delimiter=',',
converters={0:mdates.strpdate2num('%m/%d/%Y')},skiprows=1,usecols=(0,1,2,3,4),unpack=True)
plt.plot_date(date,open,'y-')
plt.plot_date(date,high,'g-')
plt.plot_date(date,low,'b-')
plt.plot_date(date,close,'r-')
plt.show()
条形图:
import numpy as np
import matplotlib.pyplot as plt
index=np.arange(5)
sales_BJ=[52,55,63,53,40]
sales_SH=[44,66,55,41,30]
bar_width=0.3
plt.barh(left=0,bottom=index,width=sales_BJ,height=bar_width,color='b')
plt.barh(left=0,bottom=index+bar_width,width=sales_SH,height=bar_width,color='r')
plt.show()
plt.barh(left=0,bottom=index,width=sales_BJ,height=bar_width,color='b')
plt.barh(left=sales_BJ,bottom=index,width=sales_SH,height=bar_width,color='r')
plt.show()
直方图:
import numpy as np
import matplotlib.pyplot as plt
mu = 10 # mean of distribution
sigma = 3 # standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.hist(x, bins=10,normed=True)
plt.hist(x, bins=50,normed=False)
plt.show()
x=np.random.randn(2000)+1
y=np.random.randn(2000)+5
plt.hist2d(x,y,bins=40)
plt.show()
饼状图:
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
# Some data
labels = 'SH', 'BJ', 'SZ', 'GD'
fracs = [20, 10, 30, 25]
explode = (0, 0, 0.05, 0)
plt.axes(aspect=1)
plt.pie(fracs, explode=explode, labels=labels, autopct='%.1f%%', shadow=True)
plt.show()
箱形图:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(100)
data = np.random.normal(size=(100, 5), loc=0.0, scale=1.0)
labels = ['A','B','C','D','E']
plt.boxplot(data, labels=labels,sym='o',whis=1.25)
plt.show()
子图:
import matplotlib.pyplot as plt
plt.subplot(121)
plt.plot(range(12))
plt.subplot(122)
plt.plot([3,2,1],[1,2,3])
plt.show()
多图:
import matplotlib.pyplot as plt
fig1=plt.figure()
ax1=fig1.add_subplot(111)
ax1.plot([1,2,3],[3,2,1])
fig2=plt.figure()
ax2=fig2.add_subplot(111)
ax2.plot([1,2,3],[1,2,3])
plt.show()
图例:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(1,11,1)
y=x*x
plt.plot(x,x*2,label='Normal')
plt.plot(x,x*3,label='Fast')
plt.plot(x,x*4,label='Faster')
plt.legend()
plt.show()
#方式2
plt.plot(x,x*2)
plt.plot(x,x*3)
plt.plot(x,x*4)
plt.legend(['Normal','Fast','Faster'])
#参数
plt.legend(bbox_to_anchor=(0,1,1,0.1),loc=3,ncol=3,mode="expand")
Matplotlib绘图教程
本文详细介绍了使用Python的Matplotlib库进行数据可视化的方法,包括散点图、折线图、条形图、直方图、饼状图、箱形图等图表的绘制,以及子图和多图的布局技巧。
3万+

被折叠的 条评论
为什么被折叠?



