修改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设计】。

相关文章

python装饰器初探(推荐)

一、含有一个装饰器 #encoding: utf-8 ############含有一个装饰器######### def outer(func): def inner(*args...

在Python的Flask框架中实现单元测试的教程

 概要 在前面的章节里我们专注于在我们的小应用程序上一步步的添加功能上。到现在为止我们有了一个带有数据库的应用程序,可以注册用户,记录用户登陆退出日志以及查看修改配置文件。 在...

Numpy之文件存取的示例代码

上一篇中我们简要带过了Numpy的数据持久化,在这一篇中将要具体说明Numpy提供的文件存取功能。Numpy可以将数组保存至二进制文件、文本文件,同时支持将多个数组保存至一个文件中。 1...

python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题

python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题

最近的答题赢钱很火爆,我也参与了几次,有些题目确实很难答,但是10秒钟的时间根本不够百度的,所以写了个辅助挂,这样可以出现题目时自动百度,这个时间也就花掉2秒钟,剩下的7、8秒钟可以进行...

python Django 创建应用过程图示详解

python Django 创建应用过程图示详解

如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”...