[英]Configurate x-axis by MaxNLocator Seaborn
I want to use MaxNLocator function to change the x-axis ticks because it is too crowded.我想使用 MaxNLocator function 来更改 x 轴刻度,因为它太拥挤了。 However, it doesn't works.但是,它不起作用。 May I know what is the problem?我可以知道是什么问题吗?
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')
Another plot:另一个 plot:
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')
Import matplotlib.ticker
and Use it in axes2
导入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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.