matplotlib如何开始刻度线,从轴原点开始留有空间 [英] matplotlib how to start ticks leaving space from the axis origin

查看:1047
本文介绍了matplotlib如何开始刻度线,从轴原点开始留有空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以简单的折线图的形式针对日期绘制非数字数据.我正在使用matplotlib.

I need to plot non-numeric data against dates as a simple line graph. I am using matplotlib.

这是一些示例代码.

import matplotlib.pyplot as plt

xticks=['Jan','Feb','Mar','April','May']
x=[1,2,3,4,5]
yticks = ['Windy', 'Sunny', 'Rainy', 'Cloudy', 'Snowy']
y=[2,1,3,5,4]

plt.plot(x,y,'bo') #.2,.1,.7,.8
plt.subplots_adjust(left =0.2)

plt.xticks(x,xticks)
plt.yticks(y,yticks)
plt.show()

我想开始刻度标签,从原点开始留一些空间.这样它们看起来不会很挤.我应该为此使用Fixedlocator吗?另外,我希望图形是一条线,显示每个点的标记,例如示例. 我该如何实现?

I want to start the tick labels leaving some space from the origin. So that they don't look very crammed. Should I be using Fixedlocator for this? Also I would like the graph to be a line showing markers for every point like in this example. How can I achieve this?

推荐答案

  • 您可以通过使用ax.set_ylim设置较低的ylim来提升"图形.
  • 可以使用marker = 'o'将标记点添加到绘图中 调用plt.plot的参数设置:
    • You can "lift" the graph by setting a lower ylim with ax.set_ylim.
    • Marker dots can be added to the plot using the marker = 'o' parameter setting in the call to plt.plot:
    • import matplotlib.pyplot as plt
      fig = plt.figure()
      ax = fig.add_subplot(1, 1, 1)
      
      xticks=['Jan','Feb','Mar','April','May']
      x=[1,2,3,4,5]
      yticks = ['Windy', 'Sunny', 'Rainy', 'Cloudy', 'Snowy']
      y=[2,1,3,5,4]
      
      plt.plot(x,y,'b-', marker = 'o') #.2,.1,.7,.8
      plt.subplots_adjust(left =0.2)
      
      plt.xticks(x,xticks)
      plt.yticks(y,yticks)
      ax.set_ylim(0.5,max(y))
      plt.show()
      

      这篇关于matplotlib如何开始刻度线,从轴原点开始留有空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆