刻度设置

参考文档:

xticks 命令

yticks 命令

以xticks为例:

matplotlib.pyplot.xticks(args, *kwargs)

获取或者设置当前刻度位置和文本的 x-limits:

return locs, labels where locs is an array of tick locations and

labels is an array of tick labels.

locs, labels = xticks()

set the locations of the xticks

xticks( arange(6) )

set the locations and labels of the xticks

xticks( arange(5), (‘Tom’, ‘Dick’, ‘Harry’, ‘Sally’, ‘Sue’) )

关键字 args ,如果有其他的参数则是 Text 属性。例如,旋转长的文本标注。

xticks( arange(12), calendar.month_name[1:13], rotation=17 )

参考文档

Tick container

Axis containers

matplotlib.axis.Axis对象负责刻度线、格网线、刻度标注和坐标轴标注的绘制工作。你可以设置y轴的左右刻度或者x轴的上下刻度。 Axis 也存储了用于自动调整,移动和放缩的数据和视觉间隔;同时 Locator 和Formatter 对象控制着刻度的位置以及以怎样的字符串呈现。

每一个 Axis 对象包含一个 label 属性以及主刻度和小刻度的列表。刻度是 XTick 和 YTick 对象,其包含着实际线和文本元素,分别代表刻度和注释。因为刻度是根据需要动态创建的,你应该通过获取方法get_major_ticks() 和 get_minor_ticks()以获取主刻度和小刻度的列表。尽管刻度包含了所有的元素,并且将会在下面代码中涵盖,Axis 方法包含了获取方法以返回刻度线、刻度标注和刻度位置等等:

In [285]: axis = ax.xaxis

In [286]: axis.get_ticklocs()

Out[286]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])

In [287]: axis.get_ticklabels()

Out[287]: 10 Text major ticklabel objects>

note there are twice as many ticklines as labels because by

default there are tick lines at the top and bottom but only tick

labels below the xaxis; this can be customized

In [288]: axis.get_ticklines()

Out[288]: 20 Line2D ticklines objects>

by default you get the major ticks back

In [291]: axis.get_ticklines()

Out[291]: 20 Line2D ticklines objects>

but you can also ask for the minor ticks

In [292]: axis.get_ticklines(minor=True)

Out[292]: 0 Line2D ticklines objects>

Tick locating and formatting

该模块包括许多类以支持完整的刻度位置和格式的配置。尽管 locators 与主刻度或小刻度没有关系,他们经由 Axis 类使用来支持主刻度和小刻度位置和格式设置。一般情况下,刻度位置和格式均已提供,通常也是最常用的形式。

默认格式

当x轴数据绘制在一个大间隔的一个小的集中区域时,默认的格式将会生效。为了减少刻度标注重叠的可能性,刻度被标注在固定间隔之间的空白区域。比如:

ax.plot(np.arange(2000, 2010), range(10))

表现形式如下:

【Python Matplotlib】设置x/y坐标轴刻度

刻度仅标注了 0-9 以及一个间隔 +2e3 。如果不希望这种形式,可以关闭默认格式设置中的间隔标注的使用。

ax.get_xaxis().get_major_formatter().set_useOffset(False)

【Python Matplotlib】设置x/y坐标轴刻度

设置 rcParam axes.formatter.useoffset=False 以在全局上关闭,或者设置不同的格式。

刻度位置

Locator 类是所有刻度 Locators 的基类。 locators 负责根据数据的范围自动调整视觉间隔,以及刻度位置的选择。 MultipleLocator 是一种有用的半自动的刻度 Locator。 你可以通过基类进行初始化设置等等。

Locator 子类定义如下:

NullLocator No ticks

FixedLocator Tick locations are fixed

IndexLocator locator for index plots (e.g., where x = range(len(y)))

LinearLocator evenly spaced ticks from min to max

LogLocator logarithmically ticks from min to max

SymmetricalLogLocator locator for use with with the symlog norm, works like the LogLocator for the part outside of the threshold and add 0 if inside the limits

MultipleLocator ticks and range are a multiple of base;either integer or float

OldAutoLocator choose a MultipleLocator and dyamically reassign it for intelligent ticking during navigation

MaxNLocator finds up to a max number of ticks at nice locations

AutoLocator MaxNLocator with simple defaults. This is the default tick locator for most plotting.

AutoMinorLocator locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. It subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.

你可以继承 Locator 定义自己的 locator。 你必须重写 _call 方法,该方法返回位置的序列,你可能也想重写 autoscale 方法以根据数据的范围设置视觉间隔。

如果你想重写默认的locator,使用上面或常用的locator任何一个, 将其传给 x 或 y axis 对象。相关的方法如下:

ax.xaxis.set_major_locator( xmajorLocator )

ax.xaxis.set_minor_locator( xminorLocator )

ax.yaxis.set_major_locator( ymajorLocator )

ax.yaxis.set_minor_locator( yminorLocator )

刻度格式

刻度格式由 Formatter 继承来的类控制。 formatter仅仅作用于单个刻度值并且返回轴的字符串。

相关的子类请参考官方文档。

同样也可以通过重写 all 方法来继承 Formatter 基类以设定自己的 formatter。

为了控制主刻度或小刻度标注的格式,使用下面任一方法:

ax.xaxis.set_major_formatter( xmajorFormatter )

ax.xaxis.set_minor_formatter( xminorFormatter )

ax.yaxis.set_major_formatter( ymajorFormatter )

ax.yaxis.set_minor_formatter( yminorFormatter )

设置刻度标注

相关文档:

相关文档

set_xticklabels()

set_yticklabels()

原型举例:

set_xticklabels(labels, fontdict=None, minor=False, **kwargs)

综合举例(1)如下:

设置指定位置的标注更改为其他的标注:

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],

[r’$-\pi$’, r’$-\pi/2$’, r’$0$’, r’$+\pi/2$’, r’$+\pi$’])

plt.yticks([-1, 0, +1],

[r’$-1$’, r’$0$’, r’$+1$’])

综合举例(2)如下:

设置坐标轴主刻度和次刻度。

#!/usr/bin/env python

#-- coding: utf-8 --

#—————————————————

#演示MatPlotLib中设置坐标轴主刻度标签和次刻度标签.

#对于次刻度显示,如果要使用默认设置只要matplotlib.pyplot.minorticks_on()

#—————————————————

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator, FormatStrFormatter

#—————————————————

xmajorLocator = MultipleLocator(20) #将x主刻度标签设置为20的倍数

xmajorFormatter = FormatStrFormatter(‘%5.1f’) #设置x轴标签文本的格式

xminorLocator = MultipleLocator(5) #将x轴次刻度标签设置为5的倍数

ymajorLocator = MultipleLocator(0.5) #将y轴主刻度标签设置为0.5的倍数

ymajorFormatter = FormatStrFormatter(‘%1.1f’) #设置y轴标签文本的格式

yminorLocator = MultipleLocator(0.1) #将此y轴次刻度标签设置为0.1的倍数

t = np.arange(0.0, 100.0, 1)

s = np.sin(0.1np.pit)np.exp(-t0.01)

ax = plt.subplot(111) #注意:一般都在ax中设置,不再plot中设置

plt.plot(t,s,’–r*’)

#设置主刻度标签的位置,标签文本的格式

ax.xaxis.set_major_locator(xmajorLocator)

ax.xaxis.set_major_formatter(xmajorFormatter)

ax.yaxis.set_major_locator(ymajorLocator)

ax.yaxis.set_major_formatter(ymajorFormatter)

#显示次刻度标签的位置,没有标签文本

ax.xaxis.set_minor_locator(xminorLocator)

ax.yaxis.set_minor_locator(yminorLocator)

ax.xaxis.grid(True, which=’major’) #x坐标轴的网格使用主刻度

ax.yaxis.grid(True, which=’minor’) #y坐标轴的网格使用次刻度

plt.show()

##########################################################

图像形式如下:

python坐标轴刻度设置_Python Matplotlib 设置x/y坐标轴刻度相关推荐

  1. python画图marker显示_python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置...

    设置输出的图片大小: figsize = 11,9 figure, ax = plt.subplots(figsize=figsize) 画简单的折线图,同时标注线的形状.名称.粗细: A,=plt. ...

  2. python画图图例字体_python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置...

    设置输出的图片大小: figsize = 11,9 figure, ax = plt.subplots(figsize=figsize) 画简单的折线图,同时标注线的形状.名称.粗细: A,=plt. ...

  3. python绘制多条不同x轴曲线_Python matplotlib 绘制双Y轴曲线图的示例代码

    Matplotlib简介 Matplotlib是非常强大的python画图工具 Matplotlib可以画图线图.散点图.等高线图.条形图.柱形图.3D图形.图形动画等. Matplotlib安装 p ...

  4. python一条竖线_python matplotlib 画一条水平直线遇到的问题

    想要的图像如下: 一开始是这样画的: import numpy as np #使用import导入模块numpy,并简写成np import matplotlib.pyplot as plt #使用i ...

  5. python多轴图_Python多子图布局与坐标轴科学计算方法,python,及,计数法

    布局方法一: import numpy as np import matplotlib.pyplot as plt def f(t): return np.exp(-t) * np.cos(2*np. ...

  6. python pyplot bar 参数_Python Matplotlib.pyplot.barh()用法及代码示例

    条形图或条形图是一种图形,用长条和长条与它们所代表的值成比例的矩形条表示数据类别.条形图可以水平或垂直绘制.条形图描述了离散类别之间的比较.曲线的一个轴代表要比较的特定类别,而另一个轴代表与那些类别相 ...

  7. python双y轴的折线图_python matplotlib实现双Y轴的实例

    python matplotlib实现双Y轴的实例 如下所示: import matplotlib.pyplot as plt import numpy as np x = np.arange(0., ...

  8. echarts折线图y轴根据数值自动_Python matplotlib 绘制双Y轴曲线图的示例代码

    双X轴的 可以理解为共享y轴 ax1=ax.twiny() ax1=plt.twiny() 双Y轴的 可以理解为共享x轴 ax1=ax.twinx() ax1=plt.twinx() 自动生成一个例子 ...

  9. python 柱状图宽度设置_Python matplotlib 柱状图实例

    学习用matplotlib绘图中,数据是我之前做的实验,隐去了关键信息. 出来的效果就是题图这样,基本可以满足柱状图的绘图要求. 完整代码及注释如下: import numpy as np impor ...

最新文章

  1. Spread for Windows Forms高级主题(7)---自定义打印的外观
  2. RV1108调试串口参数设置
  3. tcp的发送端一个小包就能打破对端的delay_ack么?
  4. 基于External-DNS的多集群Service DNS实践
  5. 获取redis中以某些字符串为前缀的KEY列表
  6. Linux FTP安装问题
  7. 从环境搭建到回归神经网络案例,带你掌握Keras
  8. Android调用系统相册、拍照以及裁剪最简单的实现(兼容7.0)
  9. 创建图片mat_OPENCV(二)——Mat类与几个函数的简介
  10. Linux 各目录的作用
  11. TCRT5000红外反射传感器
  12. 使用ArcGIS提取HWSD中的土壤属性数据
  13. 用计算机控制人造卫星属于,用计算机控制人造卫星属于 为什么人造卫星在高层大气...
  14. 业务日志告警如何做?
  15. ue富文本编辑器使用
  16. (二)基于STM32f103的I2C通信接口的EPPROM模块(24C256)读写程序详解
  17. 《那些年我们追过的Wrox精品红皮计算机图书》有奖活动
  18. C++学习课件(三)
  19. Java基础:面向对象三大特征、五大原则
  20. 模仿酷狗7(Kugou7)界面——Java版

热门文章

  1. 游戏搜索引擎 - 6617.com 内测,欢迎大家点评 :)
  2. TCAM与HASH表的差异
  3. Java的MessageDigest类、MD5算法
  4. 【搜索】洛谷 P1460 健康的荷斯坦奶牛 Healthy Holsteins
  5. 一张图掌握薛兆丰经济学讲义的精华
  6. 大数据蓝皮书:解读中国大数据发展十大趋势
  7. 大学计算机数学基础2,计算机数学基础-中国大学mooc-题库零氪
  8. 矩阵论 - 7 - 求解Ax=0:主变量、特解
  9. 查题接口 源码 php 爬题,知到网课答案WEB程序设计(PHP)查题公众号
  10. WIN10笔记本禁用启用自带键盘