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

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

如何更改圖中軸的單位?

如何更改圖中軸的單位?

鳳凰求蠱 2022-10-06 19:16:44
我正在用matplotlib, 從一些.fits文件中制作一些星系速度的數字。問題是圖中的軸以像素為單位顯示了銀河系的大小,我想將它們顯示為偏角和 RightAcension(以角度單位)。我已經知道每個像素的大小為 0.396 弧秒。如何在 X 和 Y 軸上將像素轉換為弧秒?代碼如下:############################################################################### Generally the image information is located in the Primary HDU, also known# as extension 0. Here, we use `astropy.io.fits.getdata()` to read the image# data from this first extension using the keyword argument ``ext=0``:image_data = fits.getdata(image_file, ext=0)############################################################################### The data is now stored as a 2D numpy array. Print the dimensions using the# shape attribute:print(image_data.shape)############################################################################### Display the image data:fig = plt.figure()plt.imshow(image_data, cmap='Spectral_r', origin='lower', vmin=-maior_pixel, vmax=maior_pixel)plt.colorbar()fig.suptitle(f'{gals_header["MANGAID"]}', fontsize=20, fontweight='bold')ax = fig.add_subplot(111)fig.subplots_adjust(top=0.85)ax.set_title('RC')ax.set_xlabel('pixelsx')ax.set_ylabel('pixelsy')還有更多的代碼,但我只想展示我認為相關的部分(如有必要,我可以在評論中添加更多代碼)。此代碼基于此鏈接中的示例代碼:https ://docs.astropy.org/en/stable/generated/examples/io/plot_fits-image.html#sphx-glr-download-generated-examples-io-情節適合圖像-py我已經嘗試了一些東西,比如Axes.convert_xunits一些pyplot.axes功能,但沒有任何效果(或者我只是不知道如何正確使用它們)。這就是圖像當前的樣子有人可以幫忙嗎?先感謝您。
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您可以使用任何您想要的plt.FuncFormatter對象作為刻度標簽。


這是一個示例(確實是一個非常愚蠢的示例),請參閱優秀的 Matplotlib 文檔了解詳細信息。


import matplotlib.pyplot as plt

from numpy import arange


img = arange(21*21).reshape(21,21)


ax = plt.axes()

plt.imshow(img, origin='lower')

ax.xaxis.set_major_formatter(

    plt.FuncFormatter(lambda x, pos: "$\\frac{%d}{20}$"%(200+x**2)))

http://img1.sycdn.imooc.com//633eb9570001adfa04660416.jpg

每個軸都有一個major_formatter負責生成刻度標簽的軸。

格式化程序必須是從 子類化的類的實例Formatter,上面我們使用了FuncFormatter.

要初始化 aFuncFormatter我們向它傳遞一個格式化函數,我們必須使用以下必需的特征來定義它

  • 有兩個輸入,x并且posx要格式化的橫坐標(或縱坐標),而pos可以安全地忽略,

  • 返回要用作標簽的字符串。

在示例中,函數已使用lambda語法在現場定義,其要點是格式化字符串 ( "$\\frac{%d}{20}$"%(200+x**2)),將橫坐標函數格式化為LaTeX分數,如上圖所示。

重新pos參數,據我所知,它僅用于某些方法,例如

In [69]: ff = plt.FuncFormatter(lambda x, pos: "%r ? %05.2f"%(pos,x))


In [70]: ff.format_ticks((0,4,8,12))

Out[70]: ['0 ? 00.00', '1 ? 04.00', '2 ? 08.00', '3 ? 12.00']

但通常你可以忽略pos函數體中的參數。


查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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