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

相关文章

PyQt弹出式对话框的常用方法及标准按钮类型

PyQt弹出式对话框的常用方法及标准按钮类型

PyQt之弹出式对话框(QMessageBox)的常用方法及标准按钮类型 一、控件说明 QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息...

Python实用库 PrettyTable 学习笔记

本文实例讲述了Python实用库 PrettyTable。分享给大家供大家参考,具体如下: PrettyTable安装 使用pip即可十分方便的安装PrettyTable,如下: p...

关于python中密码加盐的学习体会小结

给密码加密是什么:用户注册的密码一般网站管理人员会利用md5方法加密,这种加密方法的好处是它是单向加密的,也就是说,你只有在提前知道某一串密码对应的md5加密码,才能反推出密码是多少,虽...

使用python3批量下载rbsp数据的示例代码

使用python3批量下载rbsp数据的示例代码

1. 原始网站 https://www.rbsp-ect.lanl.gov/data_pub/rbspa/ 2. 算法说明 进入需要下载的数据所在的目录,获取并解析该目录下的信息,解析出...

Python创建系统目录的方法

本文实例讲述了Python创建系统目录的方法。分享给大家供大家参考。具体如下: Python2 mkdir在没有上级目录时创建会失败.该方法可以创建多级目录。 /temp/gapgers...