最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Matplotlib繪圖之屬性設(shè)置

 更新時間:2019年08月23日 11:41:46   作者:XHHP的博客  
這篇文章主要介紹了詳解Matplotlib繪圖之屬性設(shè)置,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧

關(guān)于Python數(shù)據(jù)分析在數(shù)學(xué)建模中的更多相關(guān)應(yīng)用:Python數(shù)據(jù)分析在數(shù)學(xué)建模中的應(yīng)用匯總(持續(xù)更新中?。?br />

(1)、導(dǎo)入庫

import matplotlib.pyplot as plt
import numpy

(2)、figure對象和subplot簡單運用

#figure對象
fig = plt.figure()   #figure是圖象對象

ax1 = fig.add_subplot(2,2,1)  #創(chuàng)建一個2*2的子圖,放在第一個位置
ax2 = fig.add_subplot(2,2,2)  #創(chuàng)建一個2*2的子圖,放在第二個位置
ax3 = fig.add_subplot(2,2,3)  #創(chuàng)建一個2*2的子圖,放在第三個位置

from numpy.random import randn
plt.plot(randn(50).cumsum(),'k--')  #'k--'告訴python要畫出黑色的虛線
ax1.hist(randn(100),bins=20,color='k',alpha=0.3)

在這里插入圖片描述

(3)、調(diào)整subplot周圍的間距

#調(diào)整subplot周圍的間距
fig,axes = plt.subplots(2,2,sharex=True,sharey=True)  #直接生成fiure對象和Axes實例(數(shù)組)
for i in range(2):
  for j in range(2):
    axes[i, j ].hist(numpy.random.randn(500),bins = 50,color='k',alpha=0.5)   #遍歷創(chuàng)建圖
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0,hspace=0)  #用于調(diào)整subplot周圍的間距

在這里插入圖片描述

(4)、顏色、標記和線型

#顏色、標記和線型
plt.figure()
##linestyle設(shè)置線型,color設(shè)置顏色,marker設(shè)置設(shè)置連接點
plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')

在這里插入圖片描述

(5)、設(shè)置標題、軸標簽,刻度以及刻度標簽

#設(shè)置標題、軸標簽,刻度以及刻度標簽
fig = plt.figure()   #創(chuàng)建figure對象
ax = fig.add_subplot(1,1,1)   #獲得Axes對象
ax.plot(numpy.random.randn(1000).cumsum()) #生成隨機數(shù)
ax.set_xticks([0,250,500,750,1000])   #設(shè)置x軸刻度
ax.set_yticks([-20,-10,0,10,20])    #設(shè)置y軸刻度
ax.set_title('My first matplotlib plot')  #設(shè)置標題
ax.set_xlabel('Xtages')   #設(shè)置x軸標簽
ax.set_ylabel('Ytages')   #設(shè)置y軸標簽

在這里插入圖片描述

(6)、添加圖例

#添加圖例
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='two')
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='three')

ax.legend(loc='best')    #loc選項可以選擇圖例的位置

在這里插入圖片描述

(7)、添加注釋

#添加注釋
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one') #label標簽用于添加圖例
plt.annotate("Important value", (55,20), xycoords='data',  #添加注釋的方法
     xytext=(5, 38),
     arrowprops=dict(arrowstyle='->'))

在這里插入圖片描述

(8)、繪制常用圖形

#繪制常用圖形
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3)  #長方形
circ = plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3)      #橢圓形

ax.add_patch(rect)   #添加到圖版中
ax.add_patch(circ)

在這里插入圖片描述

(9)、圖表的導(dǎo)出

#圖表的保存
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3)  #長方形
ax.add_patch(rect)   #添加到圖版中
fig.savefig('figpath.png',dpi = 400,bbox_inches='tight') #dpi可以控制圖象的分辨率,bbox_inches可以剪除圖表的空白部分

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

武冈市| 霸州市| 公安县| 隆化县| 喜德县| 龙南县| 扶绥县| 临汾市| 乐昌市| 唐河县| 河南省| 堆龙德庆县| 青田县| 屏山县| 囊谦县| 晴隆县| 高台县| 香港 | 新竹市| 双柏县| 巴马| 卓资县| 葵青区| 嵊泗县| 南陵县| 武邑县| 澄城县| 大悟县| 页游| 思南县| 南康市| 长治县| 呈贡县| 富阳市| 昌宁县| 拜泉县| 武隆县| 孝感市| 波密县| 洪江市| 密山市|