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

matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)

 更新時(shí)間:2021年02月22日 15:06:15   作者:mighty13  
這篇文章主要介紹了matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

matplotlib中常用的標(biāo)題主要三種:窗口標(biāo)題、圖像標(biāo)題和子圖標(biāo)題。
先通過三個(gè)案例簡要說明這三類標(biāo)題的實(shí)現(xiàn)。

窗口標(biāo)題、圖像標(biāo)題,子圖標(biāo)題(僅1個(gè)子圖)

在這里插入圖片描述

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'

fig = plt.figure()
plt.plot([1, 2])
# 設(shè)置圖像標(biāo)題
plt.suptitle("這是圖像標(biāo)題")
# 設(shè)置子圖標(biāo)題
plt.title("這是子圖標(biāo)題")
# 獲取默認(rèn)窗口標(biāo)題
current_title = fig.canvas.manager.window.windowTitle()
print("默認(rèn)窗口:",current_title)
# 設(shè)置窗口標(biāo)題方式一
fig.canvas.set_window_title("這是窗口標(biāo)題")
# 設(shè)置窗口標(biāo)題方式二
fig.canvas.manager.window.setWindowTitle("這是窗口標(biāo)題")

plt.show()

窗口標(biāo)題、圖像標(biāo)題、子圖標(biāo)題(多子圖)

使用subplot函數(shù)實(shí)現(xiàn)子圖

在這里插入圖片描述

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'

fig = plt.figure()
plt.subplot(1, 2, 1)
plt.plot([1,2,3,4], [1,4,9,16], "go") 
# 設(shè)置子圖1標(biāo)題
plt.title("子圖1標(biāo)題")

plt.subplot(122)
plt.plot([1,2,3,4], [1,4,9,16], "r^") # r^ 表示 紅色(red)三角
# 設(shè)置子圖2標(biāo)題
plt.title("子圖2標(biāo)題")
# 設(shè)置圖像標(biāo)題
plt.suptitle("圖像標(biāo)題")
# 設(shè)置窗口標(biāo)題
#fig.canvas.set_window_title("這是窗口標(biāo)題")
fig.canvas.manager.window.setWindowTitle("這是窗口標(biāo)題")

plt.show()

使用subplots函數(shù)subplots實(shí)現(xiàn)子圖

在這里插入圖片描述

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(6,6))
ax[0].plot([1,2,3,4], [1,4,9,16], "go") 
# 設(shè)置子圖1標(biāo)題
ax[0].set_title("子圖1標(biāo)題") 
ax[1].plot([1,2,3,4], [1,4,9,16], "r^") 
# 設(shè)置子圖2標(biāo)題
ax[1].set_title("子圖2標(biāo)題") 
# 設(shè)置圖像標(biāo)題
plt.suptitle("圖像標(biāo)題")
# 設(shè)置窗口標(biāo)題
fig.canvas.manager.window.setWindowTitle("這是窗口標(biāo)題")

plt.show()

原理分析

通過前面三個(gè)案例可知:

窗口標(biāo)題

設(shè)置窗口標(biāo)題可以用兩種方法:
一種是調(diào)用figure.canvas對(duì)象的set_window_title方法,一種是figure.canvas.manager.window對(duì)象的setWindowTitle方法。通過下面源碼可知,這兩種方法其實(shí)是等價(jià)的。
因此在日常實(shí)現(xiàn)過程中,關(guān)鍵是獲取當(dāng)前圖像對(duì)象(figure),即案例中的fig。該方法只有一個(gè)參數(shù),類型為字符串。
可以通過

通過figure.canvas.manager.window對(duì)象的windowTitle方法可以獲取窗口標(biāo)題。

class FigureManagerQT(FigureManagerBase):
  def set_window_title(self, title):
    self.window.setWindowTitle(title)

圖像標(biāo)題

調(diào)用plt.suptitle函數(shù)即可。根據(jù)源碼可知,plt.suptitle函數(shù)其實(shí)是調(diào)用了當(dāng)前figure對(duì)象的suptitle方法。

suptitle函數(shù)參數(shù)

  def suptitle(self, t, **kwargs):
    """
    Add a centered title to the figure.

    Parameters
    ----------
    t : str
      The title text.

    x : float, default 0.5
      The x location of the text in figure coordinates.

    y : float, default 0.98
      The y location of the text in figure coordinates.

    horizontalalignment, ha : {'center', 'left', right'}, default: 'center'
      The horizontal alignment of the text relative to (*x*, *y*).

    verticalalignment, va : {'top', 'center', 'bottom', 'baseline'}, \
default: 'top'
      The vertical alignment of the text relative to (*x*, *y*).

    fontsize, size : default: :rc:`figure.titlesize`
      The font size of the text. See `.Text.set_size` for possible
      values.

    fontweight, weight : default: :rc:`figure.titleweight`
      The font weight of the text. See `.Text.set_weight` for possible
      values.

    Returns
    -------
    text
      The `.Text` instance of the title.

    Other Parameters
    ----------------
    fontproperties : None or dict, optional
      A dict of font properties. If *fontproperties* is given the
      default values for font size and weight are taken from the
      `.FontProperties` defaults. :rc:`figure.titlesize` and
      :rc:`figure.titleweight` are ignored in this case.

    **kwargs
      Additional kwargs are `matplotlib.text.Text` properties.

    Examples
    --------
    >>> fig.suptitle('This is the figure title', fontsize=12)
    """

子圖標(biāo)題

  • 使用subplot函數(shù):在所在子圖中,使用plt.title函數(shù)。
  • 使用subplots函數(shù):使用子圖對(duì)象調(diào)用set_title方法。
  • plt.title函數(shù)和axes.set_title方法的參數(shù)相同。

注意,在使用subplots函數(shù)創(chuàng)建子圖時(shí),為什么不能使用plt.title函數(shù)設(shè)置子圖標(biāo)題呢?
根據(jù)title函數(shù)的源碼可知,title函數(shù)其實(shí)是通過gca()函數(shù)獲取子圖,然后再調(diào)用set_title方法設(shè)置標(biāo)題的。根據(jù)實(shí)驗(yàn),在使用subplots函數(shù)函數(shù)創(chuàng)建多個(gè)子圖時(shí),plt.gca()只能得到最后一個(gè)子圖的標(biāo)題,因此,在某些情況下使用plt.title函數(shù)可設(shè)置最后一個(gè)子圖的標(biāo)題。

plt.title函數(shù)和axes.set_title方法源碼

def title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs):
  return gca().set_title(
    label, fontdict=fontdict, loc=loc, pad=pad, y=y, **kwargs)
Axes.set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs):
  """
  Set a title for the axes.

  Set one of the three available axes titles. The available titles
  are positioned above the axes in the center, flush with the left
  edge, and flush with the right edge.

  Parameters
  ----------
  label : str
    Text to use for the title

  fontdict : dict
    A dictionary controlling the appearance of the title text,
    the default *fontdict* is::

      {'fontsize': rcParams['axes.titlesize'],
      'fontweight': rcParams['axes.titleweight'],
      'color': rcParams['axes.titlecolor'],
      'verticalalignment': 'baseline',
      'horizontalalignment': loc}

  loc : {'center', 'left', 'right'}, default: :rc:`axes.titlelocation`
    Which title to set.

  y : float, default: :rc:`axes.titley`
    Vertical axes loation for the title (1.0 is the top). If
    None (the default), y is determined automatically to avoid
    decorators on the axes.

  pad : float, default: :rc:`axes.titlepad`
    The offset of the title from the top of the axes, in points.

  Returns
  -------
  `.Text`
    The matplotlib text instance representing the title

  Other Parameters
  ----------------
  **kwargs : `.Text` properties
    Other keyword arguments are text properties, see `.Text` for a list
    of valid text properties.
  """

plt.gca()實(shí)驗(yàn)

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(6,6))
ax[0].plot([1,2,3,4], [1,4,9,16], "go") 
ax[1].plot([1,2,3,4], [1,4,9,16], "r^") 

print(plt.gca())
print(ax[0],ax[1])

結(jié)果為

AxesSubplot(0.547727,0.11;0.352273x0.77)
AxesSubplot(0.125,0.11;0.352273x0.77) AxesSubplot(0.547727,0.11;0.352273x0.77)

到此這篇關(guān)于matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)的文章就介紹到這了,更多相關(guān)matplotlib 標(biāo)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python出現(xiàn)

    python出現(xiàn)"IndentationError: unexpected indent"錯(cuò)誤解決辦法

    這篇文章主要介紹了python出現(xiàn)"IndentationError: unexpected indent"錯(cuò)誤解決辦法的相關(guān)資料,希望通過本文能解決遇到這樣的問題,需要的朋友可以參考下
    2017-10-10
  • python正則表達(dá)式中匹配次數(shù)與貪心問題詳解(+??*)

    python正則表達(dá)式中匹配次數(shù)與貪心問題詳解(+??*)

    正則表達(dá)式是一個(gè)特殊的字符序列,它能幫助你方便的檢查一個(gè)字符串是否與某種模式匹配,下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式中匹配次數(shù)與貪心問題(+??*)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Python?安裝教程以及快速入門

    Python?安裝教程以及快速入門

    Python是一種簡單易學(xué)的編程語言,適合初學(xué)者入門。本文將介紹Python的安裝教程以及快速入門,幫助讀者快速上手Python編程。
    2023-09-09
  • Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲完整示例

    Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲完整示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲,結(jié)合完整實(shí)例形式分析了Python爬蟲爬取文軒網(wǎng)圖書信息的相關(guān)操作技巧,需要的朋友可以參考下
    2019-05-05
  • Flask框架的學(xué)習(xí)指南之開發(fā)環(huán)境搭建

    Flask框架的學(xué)習(xí)指南之開發(fā)環(huán)境搭建

    本文是Flask框架的學(xué)習(xí)指南系列文章的第一篇,主要給大家講述的是開發(fā)環(huán)境的搭建工作,有需要的小伙伴可以參考下
    2016-11-11
  • Python基于遞歸算法實(shí)現(xiàn)的走迷宮問題

    Python基于遞歸算法實(shí)現(xiàn)的走迷宮問題

    這篇文章主要介紹了Python基于遞歸算法實(shí)現(xiàn)的走迷宮問題,結(jié)合迷宮問題簡單分析了Python遞歸算法的定義與使用技巧,需要的朋友可以參考下
    2017-08-08
  • 一文教會(huì)你用python裁剪圖片

    一文教會(huì)你用python裁剪圖片

    Python語言的圖片處理使我們常常使用的方面,那么我們?cè)撊绾螌?shí)現(xiàn)圖片的剪切呢?下面這篇文章主要給大家介紹了關(guān)于用python裁剪圖片的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Python爬取個(gè)人微信朋友信息操作示例

    Python爬取個(gè)人微信朋友信息操作示例

    這篇文章主要介紹了Python爬取個(gè)人微信朋友信息操作,涉及Python使用itchat包實(shí)現(xiàn)微信朋友信息爬取操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • python簡單實(shí)例訓(xùn)練(21~30)

    python簡單實(shí)例訓(xùn)練(21~30)

    上篇文章給大家介紹了python簡單實(shí)例訓(xùn)練的1-10,這里繼續(xù)為大家介紹python的一些用法,希望大家每個(gè)例子都打出來測試一下
    2017-11-11
  • 分享Python字符串關(guān)鍵點(diǎn)

    分享Python字符串關(guān)鍵點(diǎn)

    字符串是 Python 中最常用的數(shù)據(jù)類型。我們可以使用引號(hào)來創(chuàng)建字符串,通過本篇文章給大家分享python字符串關(guān)鍵點(diǎn)相關(guān)資料,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12

最新評(píng)論

鲁山县| 灯塔市| 香河县| 金华市| 分宜县| 黑山县| 财经| 建湖县| 苏尼特右旗| 滕州市| 晋江市| 廉江市| 佳木斯市| 静海县| 大关县| 安福县| 布尔津县| 璧山县| 多伦县| 崇左市| 濮阳县| 抚州市| 镇雄县| 左权县| 武冈市| 错那县| 乌兰察布市| 桐梓县| 滦南县| 兰考县| 司法| 田林县| 永安市| 夹江县| 边坝县| 卓资县| 松阳县| 尉氏县| 临桂县| 闻喜县| 闸北区|