如何更改matplotlib图表中x记号的密度?

gywdnpxw  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(303)

我用matplotlib绘制了一个图表,但是x记号太多了。我能知道什么解决办法吗?

from pandas_datareader import data
import datetime

tickers = 'AAPL'

dateToday = datetime.datetime.today().strftime("%Y-%m-%d")#年月日20190526

# Only get the adjusted close.

tickers_data = data.DataReader(tickers,
                       start='', 
                       end=dateToday, 
                       data_source='yahoo')[["Adj Close", "Volume"]][-250:]

returns = tickers_data.pct_change()

plt.figure(figsize=(12,6))

ax = sns.barplot(x=returns.index.strftime('%d/%-m'), y=returns['Adj Close'], color='#73a9d1')

plt.xticks(rotation = 90)
plt.title('Returns' + '\n' + tickers)

输出:

fhg3lkii

fhg3lkii1#

例如,如果您希望每五个x-tick(x-tick步骤是5)查看一次,可以通过以下方式改进代码:

step = 5
x_values = returns.index.strftime('%d/%-m')
x_ticks_values = x_values[::step]

plt.figure(figsize = (12, 6))

ax = sns.barplot(x = x_values, 
                 y = returns['Adj Close'], 
                 color = '#73a9d1')

plt.xticks(ticks = np.arange(0, (len(x_values) + step), step), 
           labels = x_ticks_values,
           rotation = 90)

最终绘图

相关问题