修改python plot折线图的坐标轴刻度方法

yipeiwu_com5年前Python基础

修改python plot折线图的坐标轴刻度,这里修改为整数:

python plot折线图的坐标轴刻度

代码如下:

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
import numpy as np


def std_plot():
 overall_std = [34.369, 21.366, 16.516, 11.151]
 max_std = [36.769, 21.794, 14.390, 4.684]
 plt.figure()
 plt.plot(overall_std, label='average_std')

 plt.plot(max_std, label='max_std')
 plt.legend()
 plt.xlabel('window')
 plt.ylabel('std')
 plt.xticks(range(len(max_std)))
 # plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%1.1f'))

 plt.show()

std_plot()

可以发现,通过上面的方法可以自定义x轴的刻度显示为其他样式,比如根据时间显示。只需要修改为:

plt.xticks(pd.date_range(‘2014-09-01','2014-09-30'),rotation=90)#设置时间标签显示格式

如果希望保留小数点后一位,可以这样:

python plot折线图的坐标轴刻度

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
import numpy as np


def std_plot():
 overall_std = [34.369, 21.366, 16.516, 11.151]
 max_std = [36.769, 21.794, 14.390, 4.684]
 plt.figure()
 plt.plot(overall_std, label='average_std')

 plt.plot(max_std, label='max_std')
 plt.legend()
 plt.xlabel('window')
 plt.ylabel('std')
 # plt.xticks(range(len(max_std)))
 plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%1.1f'))

 plt.show()


std_plot()

以上这篇修改python plot折线图的坐标轴刻度方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

10款最好的Web开发的 Python 框架

10款最好的Web开发的 Python 框架

  Python 是一门动态、面向对象语言。其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性。除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,...

python列表生成器迭代器实例解析

这篇文章主要介绍了python列表生成器迭代器实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 列表生成式 例如 有一个列表...

Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法)。分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法——通过边实现...

Python线上环境使用日志的及配置文件

目录 瞎比比 与 print 相比 logging 有什么优势? 基础用法 保存到文件 多模块使用 logging 使用配置文件配置 logging 瞎...

详解Python中的__getitem__方法与slice对象的切片操作

Fib实例虽然能作用于for循环,看起来和list有点像,但是,把它当成list来使用还是不行,比如,取第5个元素: >>> Fib()[5] Traceback...