亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pyplot - 將單個文本添加到 xaxis(如勾號)

Pyplot - 將單個文本添加到 xaxis(如勾號)

慕哥9229398 2023-06-06 14:57:00
假設您有一個在特定位置 x 處帶有垂直線 (matplotlib.axes.Axes.axvline) 的 pyplot?,F在我想在 x 的 x 軸上有一個像“COG”這樣的文本,就像它是一個勾號一樣。它可以在可見軸或不可見軸上,也可以在兩者上。然而,刻度已存在(給定數組)子圖的共享 x 軸,僅最低可見我雖然關于使用普通文本(matplotlib.pyplot.text),但是它會在子圖中它不會是 xaxis 關系(至少到目前為止我還沒有找到可行的方法)我覺得手動編輯刻度以添加單個項目并不是一個很好的解決方法。先謝謝了!
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

以下是子圖的示例圖,來自matplotlib

import matplotlib.pyplot as plt

import numpy as np


# Some example data to display

x = np.linspace(0, 2 * np.pi, 400)

y = np.sin(x ** 2)


fig, axs = plt.subplots(2, sharex=True)

fig.suptitle('Vertically stacked subplots')

axs[0].plot(x, y)

axs[1].plot(x, -y)

要添加你想要的元素,你可以使用axvlineand text; Text元素可以在圖的邊界之外(實際上刻度標簽是Text)。


#continued from above:

axs[0].xaxis.set_visible(False)

axs[0].axvline(4.5, color='red')

axs[0].text(4.5, -.05, 'COG', color='red', transform=axs[0].get_xaxis_transform(),

? ? ? ? ? ? ha='center', va='top')


axs[1].axvline(4.5, color='red')

axs[1].text(4.5, -.05, 'COG', color='red', transform=axs[1].get_xaxis_transform(),

? ? ? ? ? ? ha='center', va='top')

http://img4.sycdn.imooc.com/647ed8de000190d203680272.jpg

您可以改為添加另一個勾號并更改其顏色:


#again, continued from the first code block

axs[0].xaxis.set_visible(False)

axs[0].axvline(4.5, color='red')

axs[0].text(4.5, -.05, 'COG', color='red', transform=axs[0].get_xaxis_transform(),

? ? ? ? ? ? ha='center', va='top')


ticks = [0, 1, 2, 3, 4, 4.5, 5, 6]

labels = [0, 1, 2, 3, 4, "COG", 5, 6]

axs[1].axvline(4.5, color='red')

axs[1].set_xticks(ticks)

axs[1].set_xticklabels(labels)

axs[1].get_xticklabels()[5].set_color('red')

但是,如果您不想在頂部圖表上打勾,那么添加Text(如第一個示例中所示)似乎是最簡單的。此外,在第二個示例中手動設置刻度似乎更冗長,并且存在選擇要更改的刻度的問題(我在此處進行索引axs[1].get_xticklabels()[5],但對于更多刻度/更復雜的數字,您可能需要更智能的東西)。所以我更喜歡第一種方法而不是這種方法,但它在某些情況下可能會有用(比如如果你想讓你的線出現在現有的刻度上)。


查看完整回答
反對 回復 2023-06-06
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

使用 Toms 的第一個例子可以得到預期的結果。


此外,對于標簽上重疊文本的情況,我搜索了相鄰的刻度標簽并將其透明度設置為 != 1。因此,文本“cog”始終可見。


import matplotlib.pyplot as plt

import numpy as np


xV = 4.5

dxV = 1/4 # best 1/4 of the spacing between two ticks


# Some example data to display

x = np.linspace(0, 2 * np.pi, 400)

y = np.sin(x ** 2)


fig, axs = plt.subplots(2, sharex=True)

fig.suptitle('Vertically stacked subplots')

axs[0].plot(x, y)

axs[0].xaxis.set_visible(False)

axs[0].axvline(xV, color='red')

axs[0].text(xV, -.05, 'COG', color='red', transform=axs[0].get_xaxis_transform(),

            ha='center', va='top')

axs[1].plot(x, -y)

axs[1].axvline(xV, color='red')

axs[1].text(xV, -.05, 'COG', color='red', transform=axs[1].get_xaxis_transform(),

            ha='center', va='top')


# Change Transparency if too close

xticks = axs[1].xaxis.get_major_ticks()

values = axs[1].get_xticks()

# values = axs[1].xaxis.get_major_locator()()

pos = np.where(np.logical_and( (xV-dxV) <= values, (xV+dxV) >= values))[0]

if pos.size > 0:

    dist = np.abs(values[pos]-xV)

    pos = pos[dist.argmin()]

    xticks[pos].label1.set_alpha(0.5)


plt.show()

http://img1.sycdn.imooc.com//647ed8f50001657d03710554.jpg

查看完整回答
反對 回復 2023-06-06
  • 2 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號