日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打开figure对象_Python ——绘图 plt.figure()的使用

發布時間:2025/3/15 python 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打开figure对象_Python ——绘图 plt.figure()的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.figure語法及操作

(1)figure語法說明

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

num:圖像編號或名稱,數字為編號 ,字符串為名稱

figsize:指定figure的寬和高,單位為英寸;

dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值為80 ? ? ?1英寸等于2.5cm,A4紙是 21*30cm的紙張

facecolor:背景顏色

edgecolor:邊框顏色

frameon:是否顯示邊框

(2)例子:

import matplotlib.pyplot as plt? ??#創建自定義圖像

fig=plt.figure(figsize=(4,3),facecolor='blue')

plt.show()

2.subplot創建單個子圖

(1) subplot語法

subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)

subplot可以規劃figure劃分為n個子圖,但每條subplot命令只會創建一個子圖 ,參考下面例子。

(2)例子

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0, 100)

#作圖1

plt.subplot(221)

plt.plot(x, x)

#作圖2

plt.subplot(222)

plt.plot(x, -x)

#作圖3

plt.subplot(223)

plt.plot(x, x ** 2)

plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

#作圖4

plt.subplot(224)

plt.plot(x, np.log(x))

plt.show()

plt.plot(np.random.randn(50).cumsum(), 'k--'

3.subplots創建多個子圖

(1)subplots語法

subplots參數與subplots相似

(2)例子

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0, 100)

#劃分子圖

fig,axes=plt.subplots(2,2)

ax1=axes[0,0]

ax2=axes[0,1]

ax3=axes[1,0]

ax4=axes[1,1]

#作圖1

ax1.plot(x, x)

#作圖2

ax2.plot(x, -x)

#作圖3

ax3.plot(x, x ** 2)

ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

#作圖4

ax4.plot(x, np.log(x))

plt.show()

4.面向對象API:add_subplots與add_axes新增子圖或區域

add_subplot與add_axes都是面對象figure編程的,pyplot api中沒有此命令

(1)add_subplot新增子圖

add_subplot的參數與subplots的相似

import numpy as np

import matplotlib.pyplot as plt

x = np.arange(0, 100)

#新建figure對象

fig=plt.figure()

#新建子圖1

ax1=fig.add_subplot(2,2,1)

ax1.plot(x, x)

#新建子圖3

ax3=fig.add_subplot(2,2,3)

ax3.plot(x, x ** 2)

ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

#新建子圖4

ax4=fig.add_subplot(2,2,4)

ax4.plot(x, np.log(x))

plt.show()

可以用來做一些子圖。。。圖中圖。。。

(2)add_axes新增子區域

add_axes為新增子區域,該區域可以座落在figure內任意位置,且該區域可任意設置大小

add_axes參數可參考官方文檔:http://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure

import numpy as np

import matplotlib.pyplot as plt

#新建figure

fig = plt.figure()

# 定義數據

x = [1, 2, 3, 4, 5, 6, 7]

y = [1, 3, 4, 2, 5, 8, 6]

#新建區域ax1

#figure的百分比,從figure 10%的位置開始繪制, 寬高是figure的80%

left, bottom, width, height = 0.1, 0.1, 0.8, 0.8

# 獲得繪制的句柄

ax1 = fig.add_axes([left, bottom, width, height])

ax1.plot(x, y, 'r')

ax1.set_title('area1')

#新增區域ax2,嵌套在ax1內

left, bottom, width, height = 0.2, 0.6, 0.25, 0.25

# 獲得繪制的句柄

ax2 = fig.add_axes([left, bottom, width, height])

ax2.plot(x,y, 'b')

ax2.set_title('area2')

plt.show()

標簽:subplot,plot,plt,figure,Python,axes,add

總結

以上是生活随笔為你收集整理的python打开figure对象_Python ——绘图 plt.figure()的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。