如何在绘图,Python模式下添加颜色[Syntaxiserror]

时间:2018-09-13 01:53:50

标签: python plot

我正在尝试绘制此图:

plot((1,(x,-2,20), color='green'), (log(x),(x,-2,20), color='red'))

在python中,但是我收到了语法错误。我已经搜索了如何更改颜色,但是没有用。

先谢谢您
V

1 个答案:

答案 0 :(得分:0)

由于您大概是matplotlib的初学者,所以我为您提供了一个可行的解决方案,该解决方案在同一图中绘制了两条曲线。您可以根据需要的输入来修改x的值。如果您还有其他疑问,请随时在评论部分中问我更多。

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()

x = np.linspace(1, 20, 100)
y1 = np.log10(x)
y2 = np.ones(100)

plt.plot(x, y1, color='red', label=r'$y=\log(x)$')
plt.plot(x, y2, color='green', label=r'$y=1$')
plt.xlabel('$x$-values', fontsize=18)
plt.ylabel('$y$-values', fontsize=18)
plt.legend(fontsize=18)

输出

enter image description here