英文:
how to convert hyphen to minus for exponent in matplotlib?
问题
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(10,7))
plt.plot(history_1.history['loss'], color='coral', label='Training loss')
plt.plot(history_1.history['val_loss'], color='firebrick', label='Testing loss')
plt.title('Training and testing loss in the function of epochs', fontsize=18)
plt.xlabel('Epochs', fontsize=15)
plt.yscale('log')
plt.ylabel('Loss', fontsize=15)
plt.legend(fontsize=15)
plt.grid()
plt.show()
plt.show()
以下是输出结果:
点击这里查看图片
如何将Y轴上的连字符转换为短划线?
英文:
Consider the following example
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure(figsize = (10,7))
plt.plot(history_1.history['loss'], color = 'coral', label = 'Training loss')
plt.plot(history_1.history['val_loss'], color = 'firebrick', label = 'Testing loss')
plt.title('Training and testing loss in the function of epochs', fontsize = 18)
plt.xlabel('Epochs', fontsize = 15)
plt.yscale('log')
plt.ylabel('Loss', fontsize = 15)
plt.legend(fontsize = 15)
plt.grid()
plt.show()
plt.show()
The out put will be shown as follows
enter image description here
Any help on how can we convert the hyphen to short dash in y axis?
答案1
得分: 1
你可以像这个示例一样使用Unicode连字符。
import matplotlib.pyplot as plt
...
plt.yscale('log')
ax = plt.gca()
old_formatter = ax.yaxis.get_major_formatter()
def new_formatter(x, pos = None):
return old_formatter(x, pos).replace('-', '\u2010')
ax.yaxis.set_major_formatter(new_formatter)
...
英文:
You can use a unicode hyphen like this example.
import matplotlib.pyplot as plt
...
plt.yscale('log')
ax = plt.gca()
old_formatter = ax.yaxis.get_major_formatter()
def new_formatter(x, pos = None):
return old_formatter(x, pos).replace('-', '\u2010')
ax.yaxis.set_major_formatter(new_formatter)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论