关于python matplotlib x轴日期格式设置
About python matplotlib x-axis date format setting
关于matplotlib x轴日期格式设置
大家好:
python 我正在学习写爬虫程序
通过yfinance包获取的股票信息
使用matplotlib绘制折线图
但是matplotlib绘制的轴的日期格式
是例如2021 年 11 月 3 日,不是我想要的 matplotlib 日期格式
希望matplotlib绘制的x轴的日期格式可以改一下
改为yyyy-mm-dd格式,
希望大家帮帮忙
我的代码:
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import DateFormatter as mdates
startday = '2021-11-03'
endday = '2021-11-12'
ticker_list = ['2303.TW','2610.TW','2618.TW']
data = pd.DataFrame(columns=ticker_list)
for ticker in ticker_list:
data[ticker] = yf.download(ticker, startday,endday)['Adj Close'] #Get the closing price of the stock market on that day
ans=data.head()
#Start drawing
data.plot(figsize=(8,12))
plt.legend()
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("Line chart change of 7-day closing price of stock market", fontsize=16)
plt.ylabel('closing price', fontsize=14)
plt.xlabel('Day', fontsize=14)
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()
print(ans)
您需要定位器和格式化器。参考:https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html 而且您导入的方式 mdates
不是通常的方式,所以我也进行了调整。
# ... other imports
import matplotlib.dates as mdates
startday = '2021-11-03'
endday = '2021-11-12'
ticker_list = ['2303.TW','2610.TW','2618.TW']
data = pd.DataFrame(columns=ticker_list)
for ticker in ticker_list:
data[ticker] = yf.download(ticker, startday,endday)['Adj Close']
ans=data.head()
data.plot(figsize=(8,12))
plt.legend()
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("Line chart change of 7-day closing price of stock market",
fontsize=16)
plt.ylabel('closing price', fontsize=14)
plt.xlabel('Day', fontsize=14)
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
fmt_day = mdates.DayLocator() # provides a list of days
plt.gca().xaxis.set_major_locator(fmt_day)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
plt.show()
关于matplotlib x轴日期格式设置 大家好: python 我正在学习写爬虫程序 通过yfinance包获取的股票信息 使用matplotlib绘制折线图 但是matplotlib绘制的轴的日期格式 是例如2021 年 11 月 3 日,不是我想要的 matplotlib 日期格式
希望matplotlib绘制的x轴的日期格式可以改一下 改为yyyy-mm-dd格式, 希望大家帮帮忙
我的代码:
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import DateFormatter as mdates
startday = '2021-11-03'
endday = '2021-11-12'
ticker_list = ['2303.TW','2610.TW','2618.TW']
data = pd.DataFrame(columns=ticker_list)
for ticker in ticker_list:
data[ticker] = yf.download(ticker, startday,endday)['Adj Close'] #Get the closing price of the stock market on that day
ans=data.head()
#Start drawing
data.plot(figsize=(8,12))
plt.legend()
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("Line chart change of 7-day closing price of stock market", fontsize=16)
plt.ylabel('closing price', fontsize=14)
plt.xlabel('Day', fontsize=14)
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()
print(ans)
您需要定位器和格式化器。参考:https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html 而且您导入的方式 mdates
不是通常的方式,所以我也进行了调整。
# ... other imports
import matplotlib.dates as mdates
startday = '2021-11-03'
endday = '2021-11-12'
ticker_list = ['2303.TW','2610.TW','2618.TW']
data = pd.DataFrame(columns=ticker_list)
for ticker in ticker_list:
data[ticker] = yf.download(ticker, startday,endday)['Adj Close']
ans=data.head()
data.plot(figsize=(8,12))
plt.legend()
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("Line chart change of 7-day closing price of stock market",
fontsize=16)
plt.ylabel('closing price', fontsize=14)
plt.xlabel('Day', fontsize=14)
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
fmt_day = mdates.DayLocator() # provides a list of days
plt.gca().xaxis.set_major_locator(fmt_day)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
plt.show()