修改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基础教程之基本数据类型和变量声明介绍

变量不需要声明 Python的变量不需要声明,你可以直接输入: 复制代码 代码如下: >>>a = 10 那么你的内存里就有了一个变量a, 它的值是10,它的类型是i...

pyqt 多窗口之间的相互调用方法

* 在编程开发中,一个程序不可避免的需要多窗口操作来实现具体的功能。 实现此功能的基本步骤(以三个窗口为例,使用主窗口调用其它两个窗口) # 主窗口 from PyQt5 impor...

Python 共享变量加锁、释放详解

Python 共享变量加锁、释放详解

一、共享变量 共享变量:当多个线程访问同一个变量的时候。会产生共享变量的问题。 例子: import threading sum = 0 loopSum = 1000000 def...

python selenium UI自动化解决验证码的4种方法

本文介绍了python selenium UI自动化解决验证码的4种方法,分享给大家,具体如下: 测试环境 windows7+ firefox50+ geckodrive...

python网络编程之TCP通信实例和socketserver框架使用例子

python网络编程之TCP通信实例和socketserver框架使用例子

1.TCP是一种面向连接的可靠地协议,在一方发送数据之前,必须在双方之间建立一个连接,建立的过程需要经过三次握手,通信完成后要拆除连接,需要经过四次握手,这是由TCP的半关闭造成的,一方...