簡體   English   中英

matplotlib x標簽放置

[英]matplotlib x label placement

我需要創建一個圖表如下。 並且需要像在圖像中一樣在x軸上顯示日期。

在此輸入圖像描述

但我只能生成如下圖表。 在此輸入圖像描述

請參閱用於生成此圖表的代碼

fig, ax = plt.subplots()            
ax.plot(numpyTradeData.date, numpyTradeData.adj_close)

#Add Text in Grap
ax.text(0.5,0.1, 'Class Period\n %s - %s'%(clsStartPeriod.strftime('%B %d, 
%Y'),clsEndPeriod.strftime('%B %d, %Y')),
horizontalalignment='center',
fontsize=9, color='red',
transform=ax.transAxes)

#Fill An Area in Graph
ax.fill_between(numpyTradeData.date[classStartPeriodIndex:classEndPeriodIndex], 0, numpyTradeData.adj_close[classStartPeriodIndex:classEndPeriodIndex], facecolor='0.9', alpha='0.5')   

ax.xaxis.set_major_locator(LinearLocator(numticks=5))
ax.xaxis.set_major_formatter(dateStrFormatter)
ax.set_xlim(minDate,maxDate)
ax.set_ylabel('Share Price')

formatter = FuncFormatter(self.addDollarSymbol)
ax.yaxis.set_major_formatter(formatter)
fig.autofmt_xdate()

我知道我可以垂直添加旋轉或顯示標簽。 但無法弄清楚如何在第一張圖片中顯示標簽。

您可以使用tick對象的set_positionget_position方法。 一個小例子是:

import matplotlib.pyplot as plt
f,a = plt.subplots(1,1)
a.plot([1,2])
ticks = a.xaxis.get_ticklabels()
for tick in ticks[::2]:
    tpos = tick.get_position()
    tick.set_position((tpos[0],tpos[1]-0.05))

這會產生
在此輸入圖像描述
請注意, 0.05移位是屏幕尺寸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM