Matplotlib:不同颜色点和线的图例
请参阅以下代码:Matplotlib:不同颜色点和线的图例,matplotlib,Matplotlib,请参阅以下代码: import matplotlib.pyplot as plt import numpy as np from pylab import * graph_data = [5, 8, 7, 9] x = range(len(graph_data)) y = graph_data fig, ax = plt.subplots() # Comment the following two lines plt.plot(x, y, markersize=6, color='g', l
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
graph_data = [5, 8, 7, 9]
x = range(len(graph_data))
y = graph_data
fig, ax = plt.subplots()
# Comment the following two lines
plt.plot(x, y, markersize=6, color='g', label='blah 1')
plt.plot(x, y, 'ob', markersize=6, label='blah 2')
# ...and uncomment the following line
#plt.plot(x, y, '-ob', markersize=6, label='blah')
ax.legend()
plt.show()
filename = 'test2.pdf'
fig.savefig(filename, bbox_inches='tight')
我要做的是分别为点(o
)和连接它们的线段(-
)提供不同的颜色。我能得到它:
然而,当我尝试将标签分配给它(在图例中)时,问题就出现了。我得到的图例如下:
。。。鉴于我希望在本表格中:
后面的图例可以通过注释两条
绘图
行和取消注释下面的绘图
行获得。然而,这失去了我所寻找的颜色变化。如何解决此问题(获得正确的颜色变化和正确的图例)?通过更改标记面颜色和边缘颜色,这是非常简单的。此处color='green'
将首先应用于标记和线条。然后,您可以将标记边缘颜色(mec
)和标记面颜色(mfc
)更改为蓝色
fig, ax = plt.subplots()
plt.plot(x, y, '-o', color='green', mfc='b',
mec='b', markersize=6, label='blah')
ax.legend()
plt.show()