Python绘图之二维图与三维图详解
各位工程师累了吗? 推荐一篇可以让你技术能力达到出神入化的网站"持久男"
1.二维绘图
a. 一维数据集
用 Numpy ndarray 作为数据传入 ply
1.
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(10) print "y = %s"% y x = range(len(y)) print "x=%s"% x plt.plot(y) plt.show()
2.操纵坐标轴和增加网格及标签的函数
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(10) plt.plot(y.cumsum()) plt.grid(True) ##增加格点 plt.axis('tight') # 坐标轴适应数据量 axis 设置坐标轴 plt.show()
4. 添加标题和标签 plt.title, plt.xlabe, plt.ylabel 离散点, 线
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(20) plt.figure(figsize=(7,4)) #画布大小 plt.plot(y.cumsum(),'b',lw = 1.5) # 蓝色的线 plt.plot(y.cumsum(),'ro') #离散的点 plt.grid(True) plt.axis('tight') plt.xlabel('index') plt.ylabel('value') plt.title('A simple Plot') plt.show()
1.两个数据集绘图
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(2000) y = np.random.standard_normal((10, 2)) plt.figure(figsize=(7,5)) plt.plot(y, lw = 1.5) plt.plot(y, 'ro') plt.grid(True) plt.axis('tight') plt.xlabel('index') plt.ylabel('value') plt.title('A simple plot') plt.show()
3.使用2个 Y轴(左右)fig, ax1 = plt.subplots() ax2 = ax1.twinx()
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(2000) y = np.random.standard_normal((10, 2)) fig, ax1 = plt.subplots() # 关键代码1 plt first data set using first (left) axis plt.plot(y[:,0], lw = 1.5,label = '1st') plt.plot(y[:,0], 'ro') plt.grid(True) plt.legend(loc = 0) #图例位置自动 plt.axis('tight') plt.xlabel('index') plt.ylabel('value') plt.title('A simple plot') ax2 = ax1.twinx() #关键代码2 plt second data set using second(right) axis plt.plot(y[:,1],'g', lw = 1.5, label = '2nd') plt.plot(y[:,1], 'ro') plt.legend(loc = 0) plt.ylabel('value 2nd') plt.show()
5.左右子图
有时候,选择两个不同的图标类型来可视化数据可能是必要的或者是理想的.利用子图方法:
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(2000) y = np.random.standard_normal((10, 2)) plt.figure(figsize=(10,5)) plt.subplot(121) #两行一列,第一个图 plt.plot(y[:,0], lw = 1.5,label = '1st') plt.plot(y[:,0], 'ro') plt.grid(True) plt.legend(loc = 0) #图例位置自动 plt.axis('tight') plt.xlabel('index') plt.ylabel('value') plt.title('1st Data Set') plt.subplot(122) plt.bar(np.arange(len(y)), y[:,1],width=0.5, color='g',label = '2nc') plt.grid(True) plt.legend(loc=0) plt.axis('tight') plt.xlabel('index') plt.title('2nd Data Set') plt.show()
2.直方图 plt.hist
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(2000) y = np.random.standard_normal((1000, 2)) plt.figure(figsize=(7,5)) plt.hist(y,label=['1st','2nd'],bins=25) plt.grid(True) plt.xlabel('value') plt.ylabel('frequency') plt.title('Histogram') plt.show()
4.箱型图 boxplot
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(2000) y = np.random.standard_normal((1000, 2)) fig, ax = plt.subplots(figsize=(7,4)) plt.boxplot(y) plt.grid(True) plt.setp(ax,xticklabels=['1st' , '2nd']) plt.xlabel('value') plt.ylabel('frequency') plt.title('Histogram') plt.show()
2.金融学图表 matplotlib.finance
1.烛柱图 candlestick
#!/etc/bin/python #coding=utf-8 import matplotlib.pyplot as plt import matplotlib.finance as mpf start = (2014, 5,1) end = (2014, 7,1) quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end) # print quotes[:2] fig, ax = plt.subplots(figsize=(8,5)) fig.subplots_adjust(bottom = 0.2) mpf.candlestick(ax, quotes, width=0.6, colorup='b',colordown='r') plt.grid(True) ax.xaxis_date() #x轴上的日期 ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期倾斜 plt.show()
3.股价数据和成交量
#!/etc/bin/python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import matplotlib.finance as mpf start = (2014, 5,1) end = (2014, 7,1) quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end) # print quotes[:2] quotes = np.array(quotes) fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8,6)) mpf.candlestick(ax1, quotes, width=0.6,colorup='b',colordown='r') ax1.set_title('Yahoo Inc.') ax1.set_ylabel('index level') ax1.grid(True) ax1.xaxis_date() plt.bar(quotes[:,0] - 0.25, quotes[:, 5], width=0.5) ax2.set_ylabel('volume') ax2.grid(True) ax2.autoscale_view() plt.setp(plt.gca().get_xticklabels(),rotation=30) plt.show()
到此这篇关于Python绘图之二维图与三维图详解的文章就介绍到这了,更多相关Python绘图内容请搜索来客网以前的文章或继续浏览下面的相关文章希望大家以后多多支持来客网!