我想用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')
1条答案
按热度按时间cxfofazt1#
导入
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')