2017-11-30 4 views
1

申し訳ありませんが、これは愚かな質問ですが、私の問題の解決策を見つけることができません。 私はプロットしたいいくつかのポイントを持っており、このポイントのそれぞれは1つの変数に対応しています。私の質問は:どのように私は、それぞれのユニークな色で同じプロットの各点をプロットし、凡例をプロットすることができます。このコードは私にポイントが、すべて同じ色を与える異なる変数のプロット値

import matplotlib.patches as mpatches 
import matplotlib.pyplot as plt 

function=['a', 'b', 'c', 'd', 'e'] 
acc_scores = [0.879, 0.748, 0.984, 0.944, 0.940] 

fig, ax = plt.subplots() 
colors= ['b', 'r', 'g', 'c', 'y'] #Colors I wanted to use for each data point 
plt.plot([1,2,3,4,5], acc_scores, 'ro') 
plt.axis([0, 6, 0.5, 1]) 
ax.set_xlabel('Functions', size=18) 
ax.set_ylabel('Accuracy', size=18) 
plt.show() 

これは私がこれまで持っているコードです。

ありがとうございました!

+0

も[例]あり(https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/scatter_with_legend.html)上これについてのmatplotlibのページ。 – ImportanceOfBeingErnest

答えて

0

すべてのデータを1つの線グラフとしてプロットすると、roと指定されているため、すべて同じ色になります。各点を異なるようにするには、各点をループして個別にプロットすることができます。 labelパラメータは、凡例の作成に役立ちます。

これを試してみてください:

import matplotlib.patches as mpatches 
import matplotlib.pyplot as plt 

function=['a', 'b', 'c', 'd', 'e'] 
acc_scores = [0.879, 0.748, 0.984, 0.944, 0.940] 

fig, ax = plt.subplots() 
colors= ['b', 'r', 'g', 'c', 'y'] #Colors I wanted to use for each data point 
for x, y, c, f in zip([1,2,3,4,5], acc_scores, colors, function): 
    plt.scatter(x, y, c=c, label=f) 

plt.axis([0, 6, 0.5, 1]) 
ax.set_xlabel('Functions', size=18) 
ax.set_ylabel('Accuracy', size=18) 
plt.legend() 
plt.show() 

出力

Example Output

+0

お返事ありがとうございます – Leonor

関連する問題