简体   繁体   中英

matplotlib set y axis ticks

I am trying to set y axes ticks, the left side direction out and the right side direction in.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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