2013-09-23 52 views
2

我寫了一個簡單的繪圖程序。我遇到的問題是,對於我正在繪製的每個「數據集」,我都會在圖例中獲得兩個點。用紅色箭頭看到圖。傳說獲得多個點

enter image description here

下面的代碼:

from collections import OrderedDict 
from itertools import cycle 

import matplotlib.backends.backend_agg 
from matplotlib.figure import Figure 


def simple_scatter(data, colors='rbgcmyk', no_legend=False): 
    """Create a simple scatter plot.""" 
    data = OrderedDict(data) 
    fig = Figure() 
    matplotlib.backends.backend_agg.FigureCanvasAgg(fig) 
    ax = fig.add_subplot(111) 
    colors = cycle(colors) 
    for label, points in data.iteritems(): 
    x, y = tuple(zip(*points))[:2] 
    ax.plot(x, y, 'o', color=next(colors), label=label) 

    if not no_legend: 
    ax.legend(loc='best', shadow=True, fancybox=True) 

    return fig 


figure = simple_scatter([('Foo', ((1, 2), (3, 4))), 
         ('Bar', ((2, 3), (3, 5))), 
         ('Baz', ((2, 2.5), (3, 4.5)))]) 
figure.savefig('foo.png') 

任何想法如何得到這個下降到每個數據集一個點?

+0

我不確定,但不是因爲你使用x和y(標籤,點)?只嘗試ax.plot(x ...)? – Joohwan

+0

@Joowani - 我不確定我是否理解你的評論。 'ax.plot(x,...)'會將x值繪製爲具有單調增加的x值的「y值」(這不是我想要的)。 – mgilson

+0

@DSM - 偏差與否,這是解決方案。隨意將其標記爲重複。我認爲自己刪除了這個問題,但無論出於何種原因,我的搜索條件都沒有挖出您的答案......所以,也許這會讓其他人更容易找到答案。 – mgilson

回答

2
ax.legend(loc='best', shadow=True, fancybox=True, numpoints=1) 
+1

歡迎來到SO!雖然這個答案在技術上是_correct_並且問題是重複的,你能否給你的答案添加一些評論?鏈接到文檔?讓它成爲一個優點? – tacaswell