import matplotlibpyplot as pltimport numpy as np# y1 = 519105515149507776502875502047490815492171# y2 = 525142524345515272500773502964494785491117# y3 = 5319325178515082175080349909492808492634# 数据x =
可以使用LaTeX语法,将纵坐标标签中的负号改为 $\text{-}$ 符号,即在标签前加上 $ $ 符号,如:
plt.ylabel('Average customer delivery cost ($\text{-}$)', fontsize=12)
而将整个图字体设置为Times New Roman,则需要在代码开头加上以下语句:
plt.rcParams['font.family'] = 'Times New Roman'
完整代码如下:
import matplotlib.pyplot as plt
import numpy as np
# 将字体设置为Times New Roman
plt.rcParams['font.family'] = 'Times New Roman'
# 数据
x = np.arange(300, 650, 50)
y1 = [-0.2441,-0.2662,-0.2788,-0.2758,-0.2829,-0.2745,-0.2842]
y2 = [-0.2375,-0.2565,-0.2666,-0.2646,-0.2674,-0.2514,-0.2651]
y3 = [-0.2243,-0.2391,-0.2435,-0.2516,-0.2551,-0.2465,-0.2535]
# 折线图
plt.plot(x, y1, 'r-', label=r'$\mathit{P=6}$')
plt.plot(x, y2, 'g-', label=r'$\mathit{P=7}$')
plt.plot(x, y3, 'b-', label=r'$\mathit{P=8}$')
# 散点图
plt.scatter(x, y1, c='r', marker='o', s=20)
plt.scatter(x, y2, c='g', marker='o', s=20)
plt.scatter(x, y3, c='b', marker='o', s=20)
# 图例
plt.legend(loc='upper right')
# 标题和坐标轴
plt.xlabel('Customer scale', fontsize=12)
plt.ylabel('Average customer delivery cost ($\text{-}$)', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.tick_params(axis='both', which='minor', labelsize=10)
# 显示图形
plt.show()
``
原文地址: https://www.cveoy.top/t/topic/fiTq 著作权归作者所有。请勿转载和采集!