Python 如何在matplotlib.pyplot中设置轴的格式并包含图例?

Python 如何在matplotlib.pyplot中设置轴的格式并包含图例?,python,matplotlib,Python,Matplotlib,为图形获得了这个函数,想要格式化轴,使图形从(0,0)开始,还需要如何编写图例,以便标记哪条线属于y1,哪条线属于y2,并标记轴 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(cus

为图形获得了这个函数,想要格式化轴,使图形从(0,0)开始,还需要如何编写图例,以便标记哪条线属于y1,哪条线属于y2,并标记轴

  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() 

谢谢

您可以使用
plt.xlim(x\u低,x\u高)
在任一轴上设置限制。如果您不想手动设置上限(例如,您对当前上限感到满意),请尝试:

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])
注意这里的细微差别,我们使用axis实例。这使我们能够使用ax.get\u xlim()返回当前xlimit。这将返回一个元组
(x\u低,x\u高)
,我们使用
[1]
选择第二个元组

图例的最小示例:

plt.plot(x,y,label=“一些文本”) plt.legend()


有关图例的详细信息,请参见名为
ax
的特定axis实例的
plt.xlabel(“”)或
ax.set\u xlabel(“”
)。您不必将
ax.get\u xlim()[1]
作为
set\u xlim()
方法的第二个参数。要仅更改左限制,可以包括kwarg
left
,或者只给方法一个(位置)参数,即
ax.set_xlim(0.0)
。看。