%
符号)。 break_with_str_format = True
以在轴生成期间进行格式化
ValueError: not enough values to unpack (expected 2, got 1)
attempt_y_format = True
以在生成轴后进行格式化
n_data_points = 1000
def freq_to_relfreq(x, break_with_str_format = False):
"""
Setting `break_with_str_format` to true will generate an error
"""
y = ((x/n_data_points )*100)
if break_with_str_format:
y = f"{y}%"
return y
def relfreq_to_freq(x, break_with_str_format = False):
"""
Setting `break_with_str_format` to true will generate an error
"""
y = (x/100)*n_data_points
if break_with_str_format:
y = f"{y}%"
return y
def toy_hist(attempt_y_format = False, data = np.random.normal(0, 1, n_data_points), save_fig = False):
"""
Setting `attempt_y_format` to true will not generate and error, but will not format the axis
"""
fig, ax = plt.subplots(figsize = (20, 24))
ax.hist(data, color = "#193582", edgecolor = 'white', bins = 50, alpha = 1)
ax2 = ax.secondary_yaxis('right', functions = (freq_to_relfreq, relfreq_to_freq))
ax2.set_ylabel("Relative Frequency",fontdict = {'fontsize': 40, 'family':'Calibri'}, labelpad = 15)
ax2.tick_params(axis = "y", labelsize = 28)
ticksy2 = ax2.yaxis.get_major_ticks()
newy2_ticks = [f"{i}%" for i in ticksy2]
ax.set_ylabel('Frequency', fontdict = {'fontsize': 40, 'family':'Calibri'}, labelpad = 15)
ax.tick_params(axis = "y", labelsize = 28)
ax.tick_params(axis = "x", labelsize = 28)
if attempt_y_format:
ax2.set_yticklabels(newy2_ticks)
if save_fig:
plt.savefig('toy_hist.png')
我可以执行类似的操作以使%
包含在轴标签中,而不是刻度中,但是我真的希望在刻度中使用%
。
ax2.set_ylabel("Relative Frequency(%)",fontdict = {'fontsize': 40, 'family':'Calibri'}, labelpad = 15)