![](/img/trans.png)
[英]matplotlib FuncFormatter string formatting carries into figure coordinates
[英]The matplotlib ticker FuncFormatter
我正在嘗試為條形圖手動設置股票行情。 我正在使用FunFormatter函數。 但是,我發現FunFormmater的行為太奇怪了。 對於從0到91的X軸范圍,我發現FunFormmater返回以下內容...任何想法它是如何工作的。 這是數據文件的鏈接預先感謝
-10.0 0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0 28.805725806451605 38.374395161290316 41.22463709677419 47.128709677419344 48.55383064516128 49.36818548387095 51.20048387096774 52.42201612903225 53.439959677419345 53.439959677419345 53.03278225806451 53.643548387096764 56.08661290322579 59.75120967741935 64.63733870967741 70.54141129032257 76.85266129032257 83.16391129032257 95.58282258064514
import numpy as np
import matplotlib.pyplot as plt
import pandas as p
import matplotlib.mlab as m
import matplotlib
import matplotlib.ticker as ticker
file1=np.load('numofdays.npz')
fig,axes=plt.subplots(ncols=1)
ax=axes
x=np.arange(len(file1['arr_0']))
y=np.array(file1['arr_0'])
ax.bar(x,y)
mydates=p.DatetimeIndex(file1['arr_1'])
def mme(xx,pos=None):
print(xx)
# print(mydates[int(xx-9)].strftime('%Y-%m-%d'))
return mydates[int(xx-9)].strftime('%Y-%m-%d')
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mme))
fig.autofmt_xdate()
僅顯示十分之一的標簽用於不等間距的數據是有點危險的,因為您不知道之間會發生什么。
但是,要使腳本運行,您當然需要確保位置xx
是數組的有效索引。 例如,位置100
無效,因為您的數組只有92個元素。 為此,您可以介紹一個條件。
import numpy as np
import matplotlib.pyplot as plt
import pandas as p
import matplotlib.ticker as ticker
file1=np.load('data/numofdays.npz')
fig,ax=plt.subplots(ncols=1)
x=np.arange(len(file1['arr_0']))
y=np.array(file1['arr_0'])
ax.bar(x,y)
mydates=p.DatetimeIndex(file1['arr_1'])
def mme(xx,pos=None):
if int(xx) in x:
return mydates[int(xx)].strftime('%Y-%m-%d')
else:
return ""
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(mme))
fig.autofmt_xdate()
plt.show()
作為替代,我一定會考慮繪制實際日期。
import numpy as np
import matplotlib.pyplot as plt
import pandas as p
file1=np.load('data/numofdays.npz')
fig,ax=plt.subplots(ncols=1)
y=np.array(file1['arr_0'])
mydates = p.DatetimeIndex(file1['arr_1'])
ax.bar(mydates,y, width=60)
fig.autofmt_xdate()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.