第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

用戶警告:FixedFormatter 只能與 FixLocator 一起使用

用戶警告:FixedFormatter 只能與 FixLocator 一起使用

UYOU 2023-07-11 15:01:22
我已經(jīng)使用了很長(zhǎng)一段時(shí)間的小子例程來格式化我正在繪制的圖表的軸。舉幾個(gè)例子:def format_y_label_thousands(): # format y-axis tick labels formats    ax = plt.gca()    label_format = '{:,.0f}'    ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])def format_y_label_percent(): # format y-axis tick labels formats    ax = plt.gca()    label_format = '{:.1%}'    ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])然而,昨天更新 matplotlib 后,在調(diào)用這兩個(gè)函數(shù)中的任何一個(gè)時(shí),我收到以下警告:UserWarning: FixedFormatter should only be used together with FixedLocator  ax.set_yticklabels([label_format.format(x) for x in ax.get_yticks().tolist()])出現(xiàn)這樣的警告的原因是什么?我無法弄清楚如何查看 matplotlib 的文檔。
查看完整描述

4 回答

?
qq_笑_17

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊

解決方法:


避免警告的方法是使用 FixLocator (這是 matplotlib.ticker 的一部分)。下面我展示了繪制三個(gè)圖表的代碼。我以不同的方式格式化它們的軸。請(qǐng)注意,“set_ticks”使警告靜音,但它更改了實(shí)際的刻度位置/標(biāo)簽(我花了一些時(shí)間才弄清楚FixedLocator使用相同的信息但保持刻度位置完整)。您可以使用 x/y 來查看每個(gè)解決方案如何影響輸出。


import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

import matplotlib.ticker as mticker


mpl.rcParams['font.size'] = 6.5


x = np.array(range(1000, 5000, 500))

y = 37*x


fig, [ax1, ax2, ax3] = plt.subplots(1,3)


ax1.plot(x,y, linewidth=5, color='green')

ax2.plot(x,y, linewidth=5, color='red')

ax3.plot(x,y, linewidth=5, color='blue')


label_format = '{:,.0f}'


# nothing done to ax1 as it is a "control chart."


# fixing yticks with "set_yticks"

ticks_loc = ax2.get_yticks().tolist()

ax2.set_yticks(ax1.get_yticks().tolist())

ax2.set_yticklabels([label_format.format(x) for x in ticks_loc])


# fixing yticks with matplotlib.ticker "FixedLocator"

ticks_loc = ax3.get_yticks().tolist()

ax3.yaxis.set_major_locator(mticker.FixedLocator(ticks_loc))

ax3.set_yticklabels([label_format.format(x) for x in ticks_loc])


# fixing xticks with FixedLocator but also using MaxNLocator to avoid cramped x-labels

ax3.xaxis.set_major_locator(mticker.MaxNLocator(3))

ticks_loc = ax3.get_xticks().tolist()

ax3.xaxis.set_major_locator(mticker.FixedLocator(ticks_loc))

ax3.set_xticklabels([label_format.format(x) for x in ticks_loc])


fig.tight_layout()

plt.show()

輸出圖表:

http://img1.sycdn.imooc.com/64acfe6c00011c9804300286.jpg

顯然,像上面這樣的幾行閑置代碼(我基本上是獲取 yticks 或 xticks 并再次設(shè)置它們)只會(huì)給我的程序增加噪音。我希望刪除該警告。但是,請(qǐng)查看一些“錯(cuò)誤報(bào)告”(來自上面/下面評(píng)論中的鏈接;問題實(shí)際上不是錯(cuò)誤:它是產(chǎn)生一些問題的更新),并且管理 matplotlib 的貢獻(xiàn)者有他們的理由保留警告。

舊版本的 MATPLOTLIB:?如果您使用控制臺(tái)來控制代碼的關(guān)鍵輸出(就像我一樣),則警告消息可能會(huì)出現(xiàn)問題。因此,延遲處理該問題的一種方法是將 matplotlib 降級(jí)到版本 3.2.2。我使用 Anaconda 來管理我的 Python 包,以下是用于降級(jí) matplotlib 的命令:

conda?install?matplotlib=3.2.2

并非所有列出的版本都可用。

查看完整回答
反對(duì) 回復(fù) 2023-07-11
?
慕無忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果有人使用該函數(shù)(或 yaxis 等效函數(shù))來到這里axes.xaxis.set_ticklabels(),您不需要使用 FixLocator,您可以使用axes.xaxis.set_ticks(values_list) BEFORE axes.xaxis.set_ticklabels(labels_list)來避免此警告。



查看完整回答
反對(duì) 回復(fù) 2023-07-11
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

當(dāng)我嘗試使用定位的日期刻度旋轉(zhuǎn) X 軸上的刻度標(biāo)簽時(shí),我遇到了同樣的問題:

ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
ax.xaxis.set_major_locator(dates.DayLocator())

它使用“tick_params()”方法計(jì)算出來:

ax.tick_params(axis='x', labelrotation = 45)


查看完整回答
反對(duì) 回復(fù) 2023-07-11
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

根據(jù)這個(gè) matplotlib頁面


# FixedFormatter should only be used together with FixedLocator. 

# Otherwise, one cannot be sure where the labels will end up.

這意味著一個(gè)人應(yīng)該做


positions = [0, 1, 2, 3, 4, 5]

labels = ['A', 'B', 'C', 'D', 'E', 'F']

ax.xaxis.set_major_locator(ticker.FixedLocator(positions))

ax.xaxis.set_major_formatter(ticker.FixedFormatter(labels))

ticker.LogLocator但即使標(biāo)簽被傳遞到 ,問題仍然存在ticker.FixedFormatter。所以這種情況下的解決方案是


定義格式化函數(shù)


# FuncFormatter can be used as a decorator

@ticker.FuncFormatter

def major_formatter(x, pos):

    return f'{x:.2f}'

并將格式化程序函數(shù)傳遞給FixedFormatter


ax.xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=5))

ax.xaxis.set_major_formatter(major_formatter)

詳情請(qǐng)參閱上面的鏈接。


查看完整回答
反對(duì) 回復(fù) 2023-07-11
  • 4 回答
  • 0 關(guān)注
  • 690 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)