如何在matplotlib中将连字符转换为指数符号减号?

huangapple go评论110阅读模式
英文:

how to convert hyphen to minus for exponent in matplotlib?

问题

  1. import matplotlib.pyplot as plt
  2. import matplotlib
  3. matplotlib.rcParams['axes.unicode_minus'] = False
  4. plt.figure(figsize=(10,7))
  5. plt.plot(history_1.history['loss'], color='coral', label='Training loss')
  6. plt.plot(history_1.history['val_loss'], color='firebrick', label='Testing loss')
  7. plt.title('Training and testing loss in the function of epochs', fontsize=18)
  8. plt.xlabel('Epochs', fontsize=15)
  9. plt.yscale('log')
  10. plt.ylabel('Loss', fontsize=15)
  11. plt.legend(fontsize=15)
  12. plt.grid()
  13. plt.show()
  14. plt.show()

以下是输出结果:
点击这里查看图片

如何将Y轴上的连字符转换为短划线?

英文:

Consider the following example

  1. import matplotlib.pyplot as plt
  2. import matplotlib
  3. matplotlib.rcParams['axes.unicode_minus'] = False
  4. plt.figure(figsize = (10,7))
  5. plt.plot(history_1.history['loss'], color = 'coral', label = 'Training loss')
  6. plt.plot(history_1.history['val_loss'], color = 'firebrick', label = 'Testing loss')
  7. plt.title('Training and testing loss in the function of epochs', fontsize = 18)
  8. plt.xlabel('Epochs', fontsize = 15)
  9. plt.yscale('log')
  10. plt.ylabel('Loss', fontsize = 15)
  11. plt.legend(fontsize = 15)
  12. plt.grid()
  13. plt.show()
  14. 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连字符。

  1. import matplotlib.pyplot as plt
  2. ...
  3. plt.yscale('log')
  4. ax = plt.gca()
  5. old_formatter = ax.yaxis.get_major_formatter()
  6. def new_formatter(x, pos = None):
  7. return old_formatter(x, pos).replace('-', '\u2010')
  8. ax.yaxis.set_major_formatter(new_formatter)
  9. ...
英文:

You can use a unicode hyphen like this example.

  1. import matplotlib.pyplot as plt
  2. ...
  3. plt.yscale('log')
  4. ax = plt.gca()
  5. old_formatter = ax.yaxis.get_major_formatter()
  6. def new_formatter(x, pos = None):
  7. return old_formatter(x, pos).replace('-', '\u2010')
  8. ax.yaxis.set_major_formatter(new_formatter)
  9. ...

huangapple
  • 本文由 发表于 2023年2月10日 06:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定