校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)

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

熱門(mén)標(biāo)簽:阿里電話機(jī)器人對(duì)話 正安縣地圖標(biāo)注app 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注 qt百度地圖標(biāo)注 電銷(xiāo)機(jī)器人系統(tǒng)廠家鄭州 地圖地圖標(biāo)注有嘆號(hào) 400電話申請(qǐng)資格 遼寧智能外呼系統(tǒng)需要多少錢(qián) 螳螂科技外呼系統(tǒng)怎么用

matplotlib中常用的標(biāo)題主要三種:窗口標(biāo)題、圖像標(biāo)題和子圖標(biāo)題。
先通過(guò)三個(gè)案例簡(jiǎn)要說(shuō)明這三類標(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()

原理分析

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

窗口標(biāo)題

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

通過(guò)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í)是通過(guò)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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • matplotlib subplots 設(shè)置總圖的標(biāo)題方法
  • Python使用matplotlib模塊繪制圖像并設(shè)置標(biāo)題與坐標(biāo)軸等信息示例
  • Python使用Matplotlib模塊時(shí)坐標(biāo)軸標(biāo)題中文及各種特殊符號(hào)顯示方法

標(biāo)簽:濟(jì)源 信陽(yáng) 阜新 隨州 淘寶好評(píng)回訪 昭通 合肥 興安盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)》,本文關(guān)鍵詞  matplotlib,源碼,解析,標(biāo)題,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于matplotlib源碼解析標(biāo)題實(shí)現(xiàn)(窗口標(biāo)題,標(biāo)題,子圖標(biāo)題不同之間的差異)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 临洮县| 红原县| 安吉县| 抚松县| 饶平县| 禹城市| 黄平县| 广平县| 贵阳市| 安阳县| 中方县| 恩施市| 新乡县| 巴马| 元江| 永定县| 江阴市| 霍城县| 蕉岭县| 桦甸市| 瑞金市| 黄冈市| 舟曲县| 敖汉旗| 宁武县| 深泽县| 芜湖县| 宜兰县| 新竹市| 凤冈县| 宜春市| 旬阳县| 无极县| 蒙城县| 肥乡县| 齐河县| 南投县| 呼图壁县| 永平县| 平江县| 玛沁县|