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

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

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

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

窗口標題、圖像標題,子圖標題(僅1個子圖)

在這里插入圖片描述

import matplotlib.pyplot as plt

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

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

plt.show()

窗口標題、圖像標題、子圖標題(多子圖)

使用subplot函數(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") 
# 設置子圖1標題
plt.title("子圖1標題")

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

plt.show()

使用subplots函數(shù)subplots實現(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") 
# 設置子圖1標題
ax[0].set_title("子圖1標題") 
ax[1].plot([1,2,3,4], [1,4,9,16], "r^") 
# 設置子圖2標題
ax[1].set_title("子圖2標題") 
# 設置圖像標題
plt.suptitle("圖像標題")
# 設置窗口標題
fig.canvas.manager.window.setWindowTitle("這是窗口標題")

plt.show()

原理分析

通過前面三個案例可知:

窗口標題

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

通過figure.canvas.manager.window對象的windowTitle方法可以獲取窗口標題。

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

圖像標題

調用plt.suptitle函數(shù)即可。根據(jù)源碼可知,plt.suptitle函數(shù)其實是調用了當前figure對象的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)
    """

子圖標題

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

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

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()實驗

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])

結果為

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

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

相關文章

  • python出現(xiàn)

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

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

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

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

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

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

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

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

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

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

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

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

    一文教會你用python裁剪圖片

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

    Python爬取個人微信朋友信息操作示例

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

    python簡單實例訓練(21~30)

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

    分享Python字符串關鍵點

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

最新評論

武隆县| 咸丰县| 临漳县| 辉南县| 芷江| 于都县| 磴口县| 兰考县| 宣威市| 嵊州市| 滁州市| 广河县| 绥中县| 鄂托克旗| 商城县| 昌邑市| 紫金县| 六枝特区| 巴林右旗| 扬中市| 寿阳县| 兴隆县| 甘泉县| 罗甸县| 北碚区| 凤冈县| 沁阳市| 土默特右旗| 鹤峰县| 寿光市| 神池县| 治县。| 洪泽县| 贺州市| 伊金霍洛旗| 上饶县| 尚义县| 鄱阳县| 湄潭县| 彭阳县| 荆州市|