import random import matplotlib.pyplot as plt import numpy as np from matplotlib.font_manager import FontProperties X = [30, 60, 90, 120, 150, 180, 210] Y1 = [0.3, 0.5, 0.6, 0.7, 0.7, 0.8, 0.9] Y2 = [1.5, 11.3, 25.7, 58.3, 202.6, 345.2, 456.6] plt.plot(X, Y1, label='"Multiple cycles of exploration"') plt.plot(X, Y2, label='The optimal algorithm') plt.xlabel("The number of $t_{i}$") plt.ylabel("Running time/min") plt.legend() plt.show() 如何修改程序,使x轴只在30, 60, 90, 120, 150, 180, 210显示坐标?如何在折线图中相应的坐标上画点
时间: 2024-03-16 07:48:01 浏览: 65
matplotlib.pyplot绘图显示控制方法
5星 · 资源好评率100%
要使x轴只显示指定的坐标,可以使用xticks函数来设置,如下所示:
```python
import random
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
X = [30, 60, 90, 120, 150, 180, 210]
Y1 = [0.3, 0.5, 0.6, 0.7, 0.7, 0.8, 0.9]
Y2 = [1.5, 11.3, 25.7, 58.3, 202.6, 345.2, 456.6]
plt.plot(X, Y1, label='"Multiple cycles of exploration"')
plt.plot(X, Y2, label='The optimal algorithm')
plt.xlabel("The number of $t_{i}$")
plt.ylabel("Running time/min")
plt.legend()
plt.xticks(X) # 设置x轴坐标为指定值
plt.show()
```
要在折线图中相应的坐标上画点,可以使用scatter函数来实现,如下所示:
```python
import random
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
X = [30, 60, 90, 120, 150, 180, 210]
Y1 = [0.3, 0.5, 0.6, 0.7, 0.7, 0.8, 0.9]
Y2 = [1.5, 11.3, 25.7, 58.3, 202.6, 345.2, 456.6]
plt.plot(X, Y1, label='"Multiple cycles of exploration"')
plt.plot(X, Y2, label='The optimal algorithm')
plt.xlabel("The number of $t_{i}$")
plt.ylabel("Running time/min")
plt.legend()
plt.xticks(X) # 设置x轴坐标为指定值
for i in range(len(X)):
plt.scatter(X[i], Y1[i], color='blue')
plt.scatter(X[i], Y2[i], color='orange')
plt.show()
```
这样就可以在相应的坐标上画点了。
阅读全文