【问题标题】:Configurate x-axis by MaxNLocator Seaborn通过 MaxNLocator Seaborn 配置 x 轴
【发布时间】:2021-07-20 15:37:17
【问题描述】:

我想使用 MaxNLocator 函数来更改 x 轴刻度,因为它太拥挤了。但是,它不起作用。我可以知道是什么问题吗?

from pandas_datareader import data
import datetime

tickers = 'MP'

dateToday = datetime.datetime.today().strftime("%Y-%m-%d")

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

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
# sns.set_style("darkgrid")

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

sns.barplot(x=tickers_data.index.strftime('%d/%-m'), y=tickers_data['Volume'], color='#73a9d1')
plt.MaxNLocator(10)
plt.xticks(rotation = 90)
plt.ticklabel_format(style='plain', axis='y')
plt.ylabel('Volume')
plt.title(tickers)

axes2=plt.twinx()
axes2.plot(tickers_data.index.strftime('%d/%-m'), tickers_data['Adj Close'], color='b', linewidth=0.5)
axes2.set_ylabel('Price')

输出:

另一个情节:

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

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

plt.xticks(rotation = 90)
ax.xaxis.set_major_locator(tic.MaxNLocator(nbins=60))
plt.title('returns')

【问题讨论】:

    标签: python pandas dataframe seaborn


    【解决方案1】:

    导入matplotlib.ticker并在axes2中使用

    from pandas_datareader import data
    import datetime
    
    tickers = 'MP'
    
    dateToday = datetime.datetime.today().strftime("%Y-%m-%d")
    
    # Only get the adjusted close.
    tickers_data = data.DataReader(tickers,
                           start='', 
                           end=dateToday, 
                           data_source='yahoo')[["Adj Close", "Volume"]][-250:]
    
    import matplotlib.ticker as tic # Imported Ticker
    import matplotlib.pyplot as plt
    %matplotlib inline
    import seaborn as sns
    # sns.set_style("darkgrid")
    
    plt.figure(figsize=(12,6))
    
    sns.barplot(x=tickers_data.index.strftime('%d/%-m'), y=tickers_data['Volume'], color='#73a9d1')
    plt.xticks(rotation = 90)
    plt.ticklabel_format(style='plain', axis='y')
    plt.ylabel('Volume')
    plt.title(tickers)
    
    axes2=plt.twinx()
    axes2.plot(tickers_data.index.strftime('%d/%-m'), tickers_data['Adj Close'], color='b', linewidth=0.5)
    axes2.xaxis.set_major_locator(tic.MaxNLocator(nbins=10)) #Added Here
    axes2.set_ylabel('Price')
    

    【讨论】:

    • 感谢您的帮助。但是,有一个查看问题,它不显示第一个和最后两个数字。我可以知道如何解决它吗?
    • 你能解释更多吗?哪个数字?
    • 你好。如附图所示,x 轴上的前 2 位数字和后 3 位数字缺失
    • 你是说蜱虫?您有该日期的可用数据吗?
    • 使用plt.xlim([0,len(tickers_data)-1])设置限制
    猜你喜欢
    • 2018-03-12
    • 2017-09-24
    • 2018-07-18
    • 2021-06-24
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多