2013-07-25 60 views
0

得到這個函數的圖形,想要格式軸,使圖形從(0,0)開始,我怎麼寫傳說,所以我可以標籤哪條線屬於y1和哪個y2和標籤軸。如何在matplotlib.pyplot中格式化軸幷包含圖例?

import matplotlib.pyplot as plt 
    def graph_cust(cust_type): 
    """function produces a graph of day agaist customer number for a given customer type""" 
    s = show_all_states_list(cust_type) 
    x = list(i['day']for i in s) 
    y1 = list(i['custtypeA_nondp'] for i in s) 
    y2 = list(i['custtypeA_dp']for i in s) 
    plt.scatter(x,y1,color= 'k') 
    plt.scatter(x,y2,color='g') 
    plt.show() 

由於

回答

0

可以設置使用plt.xlim(x_low, x_high)任一軸的限制。如果你不想手動設置上限(例如你的快樂與當前上限),然後嘗試:

ax = plt.subplot(111) # Create axis instance 
ax.scatter(x, y1, color='k') # Same as you have above but use ax instead of plt 
ax.set_xlim(0.0, ax.get_xlim()[1]) 

這裏注意細微的差別,我們使用的軸實例。這使我們能夠使用ax.get_xlim()返回當前的xlimits,這返回一個元組(x_low, x_high),我們使用[1]選擇第二個。

圖例的最小例如:

plt.plot(X,Y,標籤= 「一些文本」) plt.legend()

更多關於圖例見any of these examples

+0

'對於名爲ax的特定軸實例,使用plt.xlabel(「」)或者ax.set_xlabel(「」)「。 – Greg

+1

您不必在'set_xlim()'方法中包含'ax.get_xlim()[1]'作爲第二個參數。要只改變左邊極限,可以包含kwarg'left',或者只給方法一個(位置)參數,即'ax.set_xlim(0.0)'。請參閱[文檔](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_xlim)。 – hooy