Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:19:16+00:00 2026-05-30T22:19:16+00:00

In the figure below, each unit in the x-axis represents a 10mins interval. I

  • 0

In the figure below, each unit in the x-axis represents a 10mins interval. I would like to customize the labels of x-axis, so that it shows hours, i.e. it displays a ticker every 6 units (60mins). I am new to matplotlib. Could someone help me? Thanks~
enter image description here

Here is the code for the above figure.

x = arange(0, size_x, dx)
y = arange(0, size_y, dy)
X,Y = meshgrid(x, y)
Z = foo(x,y)
pcolor(X, Y, Z, cmap=cm.Reds)
colorbar()
axis([0,size_x-1,0,size_y-1])
show()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:19:17+00:00Added an answer on May 30, 2026 at 10:19 pm

    There’s more than one way to do this.

    Let’s start out with an example plot:

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    import numpy as np
    
    # Generate some data...
    x, y = np.mgrid[:141, :101]
    z = np.cos(np.hypot(x, y))
    
    # Plot the figure...
    plt.pcolormesh(x, y, z, cmap=mpl.cm.Reds)
    
    plt.show()
    

    enter image description here

    The simple way to do what you want would be something like this:

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    import numpy as np
    
    # Generate some data...
    x, y = np.mgrid[:141, :101]
    z = np.cos(np.hypot(x, y))
    
    # Plot the figure...
    plt.pcolormesh(x, y, z, cmap=mpl.cm.Reds)
    
    # Set the ticks and labels...
    ticks = np.arange(x.min(), x.max(), 6)
    labels = range(ticks.size)
    plt.xticks(ticks, labels)
    plt.xlabel('Hours')
    
    plt.show()
    

    enter image description here

    The other way involves subclassing matplotlib’s locators and tickers.

    For your purposes, the example above is fine.

    The advantage of making new locators and tickers is that the axis will automatically be scaled into reasonable intervals of the “dx” units you specify. If you’re using it as a part of a larger application, it can be worthwhile. For a single plot, it’s more trouble than it’s worth.

    If you really wanted to go that route, though, you’d do something like this:

    import matplotlib.pyplot as plt
    import matplotlib as mpl
    import numpy as np
    
    def main():
        # Generate some data...
        x, y = np.mgrid[:141, :101]
        z = np.cos(np.hypot(x, y))
    
        # Plot the figure...
        fig, ax = plt.subplots()
        ax.pcolormesh(x, y, z, cmap=mpl.cm.Reds)
        ax.set_xlabel('Hours')
    
        ax.xaxis.set_major_locator(ScaledLocator(dx=6))
        ax.xaxis.set_major_formatter(ScaledFormatter(dx=6))
    
        plt.show()
    
    class ScaledLocator(mpl.ticker.MaxNLocator):
        """
        Locates regular intervals along an axis scaled by *dx* and shifted by
        *x0*. For example, this would locate minutes on an axis plotted in seconds
        if dx=60.  This differs from MultipleLocator in that an approriate interval
        of dx units will be chosen similar to the default MaxNLocator.
        """
        def __init__(self, dx=1.0, x0=0.0):
            self.dx = dx
            self.x0 = x0
            mpl.ticker.MaxNLocator.__init__(self, nbins=9, steps=[1, 2, 5, 10])
    
        def rescale(self, x):
            return x / self.dx + self.x0
        def inv_rescale(self, x):
            return  (x - self.x0) * self.dx
    
        def __call__(self): 
            vmin, vmax = self.axis.get_view_interval()
            vmin, vmax = self.rescale(vmin), self.rescale(vmax)
            vmin, vmax = mpl.transforms.nonsingular(vmin, vmax, expander = 0.05)
            locs = self.bin_boundaries(vmin, vmax)
            locs = self.inv_rescale(locs)
            prune = self._prune
            if prune=='lower':
                locs = locs[1:]
            elif prune=='upper':
                locs = locs[:-1]
            elif prune=='both':
                locs = locs[1:-1]
            return self.raise_if_exceeds(locs)
    
    class ScaledFormatter(mpl.ticker.OldScalarFormatter):
        """Formats tick labels scaled by *dx* and shifted by *x0*."""
        def __init__(self, dx=1.0, x0=0.0, **kwargs):
            self.dx, self.x0 = dx, x0
    
        def rescale(self, x):
            return x / self.dx + self.x0
    
        def __call__(self, x, pos=None):
            xmin, xmax = self.axis.get_view_interval()
            xmin, xmax = self.rescale(xmin), self.rescale(xmax)
            d = abs(xmax - xmin)
            x = self.rescale(x)
            s = self.pprint_val(x, d)
            return s
    
    if __name__ == '__main__':
        main()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a time series(a csv file) data that looks like below. Each observation
Please help me figure a single query that will transform the data below... |id
Using the below XML, I need to figure out which person worked more hours
Im trying to for each loop true @participants collection like below: - (1..@participants.count).each do
I'm struggeling to figure out how to replace the two foreach below. public void
After loosing much sleep I still cannot figure this out: The code below (its
Note my code below. I am trying to figure out why my data is
Can't seem to figure this out, see code below. Trying to make a GET
I'm trying to figure out how to replace binary data using Java. below is
I'm trying to figure out how big a certain database would be (it hasn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.