如何在matplotlib.pyplot中格式化轴并包含图例?

2024-09-27 00:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

得到了这个图形的函数,想要格式化轴,使图形从(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() 

谢谢


Tags: in标记图形fortypeshowpltcustomer
1条回答
网友
1楼 · 发布于 2024-09-27 00:17:55

您可以使用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])

注意这里的细微差别,我们使用一个axis实例。这使我们能够使用ax.get_xlim()返回当前的xlimit。这将返回一个元组(x_low, x_high),我们使用[1]选择第二个元组。在

一个最小的图例示例:

在plt.绘图(x,y,label=“一些文本”) plt.图例()

有关图例的详细信息,请参见any of these examples

相关问题 更多 >

    热门问题