yaxis.set_major_formatter 中的 f 字符串
f-string in yaxis.set_major_formatter
我有以下代码:
import pandas as pd
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import yfinance as yf
import matplotlib.ticker as mtick
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
import currency
import warnings
warnings.filterwarnings("ignore")
start = datetime.date(2000,1,1)
end = datetime.date.today()
stock = 'goog'
fig, ax = plt.subplots(dpi=300, figsize =(8,4) )
data = web.DataReader(stock, 'yahoo', start, end)
data['Close'].plot()
ax.tick_params(axis='y', colors='midnightblue')
ax.tick_params(axis='x', colors="k")
pg = yf.Ticker(stock)
# sn = pg.info['shoName']
sn = pg.info['shortName']
b = pg.info['currency']
c = currency.symbol(f"{b}")
ax.set_ylabel(f"Price ({pg.info['currency']})")
ax.xaxis.grid(False, which='minor')
ax.yaxis.set_major_formatter('${x:1.2f}')
ax.margins(x=0)
# plt.savefig(f"{sn} {end.strftime('%d - %b%Y')}", bbox_inches='tight', dpi = 500)
print(sn)
print('datetime date =', start)
plt.show()
print()
我面临的问题是ax.yaxis.set_major_formatter('${x:1.2f}')
。我需要一个 f-string 来评估 c
。这将给出任何国家的货币而不是使用 $ 符号。但是它似乎没有评估 f-string,你能建议任何可能的替代方案吗?
tick string formatter 需要格式字符串 x
(以及可选的 pos
):
The field used for the tick value must be labeled x
and the field used for the tick position must be labeled pos
.
这意味着我们需要评估 c
但 而不是 x
,所以:
将 c
与格式字符串连接起来:
ax.yaxis.set_major_formatter(c + '{x:1.2f}')
或者传递一个评估的 f 字符串(添加 f
),其中 x
的大括号被转义([=16= 的单大括号,[= =12=]):
ax.yaxis.set_major_formatter(f'{c}{{x:1.2f}}')
我有以下代码:
import pandas as pd
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import yfinance as yf
import matplotlib.ticker as mtick
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
import currency
import warnings
warnings.filterwarnings("ignore")
start = datetime.date(2000,1,1)
end = datetime.date.today()
stock = 'goog'
fig, ax = plt.subplots(dpi=300, figsize =(8,4) )
data = web.DataReader(stock, 'yahoo', start, end)
data['Close'].plot()
ax.tick_params(axis='y', colors='midnightblue')
ax.tick_params(axis='x', colors="k")
pg = yf.Ticker(stock)
# sn = pg.info['shoName']
sn = pg.info['shortName']
b = pg.info['currency']
c = currency.symbol(f"{b}")
ax.set_ylabel(f"Price ({pg.info['currency']})")
ax.xaxis.grid(False, which='minor')
ax.yaxis.set_major_formatter('${x:1.2f}')
ax.margins(x=0)
# plt.savefig(f"{sn} {end.strftime('%d - %b%Y')}", bbox_inches='tight', dpi = 500)
print(sn)
print('datetime date =', start)
plt.show()
print()
我面临的问题是ax.yaxis.set_major_formatter('${x:1.2f}')
。我需要一个 f-string 来评估 c
。这将给出任何国家的货币而不是使用 $ 符号。但是它似乎没有评估 f-string,你能建议任何可能的替代方案吗?
tick string formatter 需要格式字符串 x
(以及可选的 pos
):
The field used for the tick value must be labeled
x
and the field used for the tick position must be labeledpos
.
这意味着我们需要评估 c
但 而不是 x
,所以:
将
c
与格式字符串连接起来:ax.yaxis.set_major_formatter(c + '{x:1.2f}')
或者传递一个评估的 f 字符串(添加
f
),其中x
的大括号被转义([=16= 的单大括号,[= =12=]):ax.yaxis.set_major_formatter(f'{c}{{x:1.2f}}')