简体   繁体   English

matplotlib设置y轴刻度

[英]matplotlib set y axis ticks

I am trying to set y axes ticks, the left side direction out and the right side direction in. 我正在尝试设置y轴刻度,将左侧方向设置为刻度,将右侧方向设置为刻度。

here's a piece of code that might help you understanding what I am trying to do 这是一段代码,可以帮助您了解我正在尝试做的事情

import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
from matplotlib.axes import Axes

class Frame(wx.Frame):
    def __init__(self):
        # initialize
        wx.Frame.__init__(self, None)
        # create main sizer
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        # create figure, canvas and matplotlib toolbar
        self.figure = Figure(figsize=(4.5,4), dpi=None)
        self.canvas = FigureCanvasWxAgg( self, -1, self.figure )
        # add canvas to sizer
        mainSizer.Add(self.canvas, proportion=1, flag=wx.ALL|wx.EXPAND)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        self.Show(True)  

app = wx.App(0)        
frame = Frame()
axes = frame.figure.add_axes((0.1,0.1,0.8,0.8))

axes.tick_params(reset=True)
axes.tick_params(axis='y',which="major", direction="in", 
                 left=True,reset=False, labelleft=True)
axes.tick_params(axis='y',which="major", direction="out", 
                 right=True,reset=False, labelright=True)

frame.canvas.draw()

app.MainLoop()

在此处输入图片说明

normally i want left ticks to be inside the axis and the right ticks to be outside 通常我希望左刻度线位于轴内,而右刻度线位于轴外

This might be a bit tricky, others more expert may be able to dig really deep into the interface to find something. 这可能有点棘手,其他一些更熟练的专家也许能够真正深入到界面中以找到一些东西。 In the meantime one option would be to use a twinned axis so that you can set the right tick properties without having to worry about affecting the left ticks. 同时,一种选择是使用双轴,以便您可以设置右刻度线属性,而不必担心会影响左刻度线。 Eg the following (in which I pulled out the pertinent parts of your code): 例如以下内容(我在其中抽出了代码的相关部分):

import matplotlib.pyplot as plt

fig = plt.figure()
axes = fig.add_axes((0.1,0.1,0.8,0.8))

axes.tick_params(axis='y',which="major", direction="in", 
                 left=True,reset=False, labelleft=True)

axes2 = axes.twinx()
axes2.tick_params(axis='y', which='major', direction='out')

plt.show()

If you do this all the time, though, it feels like a hack so may not be the best option... 但是,如果您一直这样做,那感觉就像是黑客一样,因此可能不是最佳选择...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM