覆盖 y 轴刻度标签而不影响 pyplot 中的图形形状 [英] override y axis tick labels without affecting the graph shape in pyplot

查看:34
本文介绍了覆盖 y 轴刻度标签而不影响 pyplot 中的图形形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动覆盖y轴刻度标签而不影响原始图的内容.例如,如何在不影响原始绘图形状的情况下显示 y 轴刻度标签 [1,10,100,1000,10000],即仍然显示完美的二次曲线.

what I want to manually override the y axis tick labels without affecting the original plot. For example, how I can show y axis tick labels [1,10,100,1000,10000] instead without affecting the original plot shape, i.e, still show a perfect quadratic curve.

import numpy as np
import pylab as pl
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
pl.plot(x, y)
pl.title(’Plot of y vs. x’)
pl.xlabel(’x axis’)
pl.ylabel(’y axis’)
pl.show()

我尝试了以下操作,但这不起作用

I tried the following and it does not work

newYlabel = ['1','10','100','1000','10000']
p1.set_yticklabels(newYlabel)

推荐答案

set_yticklabelsAxes 实例的方法,pylab界面.试试吧

set_yticklabels is the method of Axes instance, and is not present in the pylab interface. Try instead

pl.gca().set_yticklabels(newYlabel)

gca 在这里代表获取当前轴.

编辑:图片:

用于获取它的脚本:

import numpy as np
import pylab as pl
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
pl.plot(x, y)
pl.title('Plot of y vs. x')
pl.xlabel('x axis')
pl.ylabel('y axis')
newYlabel = ['1','10','100','1000','10000']
pl.gca().set_yticklabels(newYlabel)
pl.show()

在 Fedora 21 上使用 matplotlib 1.4.3 和 python 2.7.8.

Used matplotlib 1.4.3 with python 2.7.8 on Fedora 21.

这篇关于覆盖 y 轴刻度标签而不影响 pyplot 中的图形形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆