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

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

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)
...

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:

确定