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

  • Home
  • SEARCH
  • 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 6930955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:30:53+00:00 2026-05-27T11:30:53+00:00

I am trying to make a 2D density plot (from some simulation data) with

  • 0

I am trying to make a 2D density plot (from some simulation data) with matplotlib. My x and y data are defined as the log10 of some quantities. How can I get logarithmic axes (with log minor ticks)?

Here is an exemple of my code:

import numpy as np
import matplotlib.pyplot as plt

Data = np.genfromtxt("data") # A 2-column data file
x = np.log10(Data[:,0])
y = np.log10(Data[:,1])

xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()

fig = plt.figure()
ax = fig.add_subplot(111)

hist = ax.hexbin(x,y,bins='log', gridsize=(30,30), cmap=cm.Reds)
ax.axis([xmin, xmax, ymin, ymax])

plt.savefig('plot.pdf')
  • 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-27T11:30:54+00:00Added an answer on May 27, 2026 at 11:30 am

    Thank you very much for suggestions.

    Below, I join my own solution. It is hardly “a minimum working example” but I have already stripped my script quite a lot!

    In a nutshell, I used imshow to plot the “image” (a 2D histogram with log bins) and I remove the axes. Then, I draw a second, empty (and transparent), plot, exactly on top of the first plot just to get log axes as imshow doesn’t seem to allow it. Quite complicated if you ask me!

    My code is probably far from optimal as I am new to python and matplotlib…

    By the way, I don’t use hexbin for two reasons:
    1) It is too slow to run on very big data files like the kind I have.
    2) With the version I use, the hexagons are slightly too large, i.e. they overlap, resulting in “pixels” of irregular shapes and sizes.
    Also, I want to be able to write the histogram data into a file in text format.

    #!/usr/bin/python
    
    # How to get log axis with a 2D colormap (i.e. an "image") ??
    #############################################################
    #############################################################
    
    import numpy as np
    import matplotlib.cm as cm
    import matplotlib.pyplot as plt
    import math
    
    # Data file containing 2D data in log-log coordinates.
    # The format of the file is 3 columns : x y v
    # where v is the value to plotted for coordinate (x,y)
    # x and y are already log values
    # For instance, this can be a 2D histogram with log bins.
    input_file="histo2d.dat"
    
    # Parameters to set space for the plot ("bounding box")
    x1_bb, y1_bb, x2_bb, y2_bb = 0.125, 0.12, 0.8, 0.925
    
    # Parameters to set space for colorbar
    cb_fraction=0.15
    cb_pad=0.05
    
    # Return unique values from a sorted list, will be required later
    def uniq(seq, idfun=None): 
        # order preserving
        if idfun is None:
            def idfun(x): return x
        seen = {}
        result = []
        for item in seq:
            marker = idfun(item)
            # in old Python versions:
            # if seen.has_key(marker)
            # but in new ones:
            if marker in seen: continue
            seen[marker] = 1
            result.append(item)
        return result
    
    # Read data from file. The format of the file is 3 columns : x y v
    # where v is the value to plotted for coordinate (x,y)
    
    Data = np.genfromtxt(input_file)
    x = Data[:,0]
    y = Data[:,1]
    v = Data[:,2]
    
    # Determine x and y limits and resolution of data
    
    x_uniq = np.array(uniq(np.sort(x)))
    y_uniq = np.array(uniq(np.sort(y)))
    
    x_resolution = x_uniq.size
    y_resolution = y_uniq.size
    
    x_interval_length = x_uniq[1]-x_uniq[0]
    y_interval_length = y_uniq[1]-y_uniq[0]
    
    xmin = x.min()
    xmax = x.max()+0.5*x_interval_length
    ymin = y.min()
    ymax = y.max()+0.5*y_interval_length
    
    # Reshape 1D data to turn it into a 2D "image"
    
    v = v.reshape([x_resolution, y_resolution])
    v = v[:,range(y_resolution-1,-1,-1)].transpose()
    
    # Plot 2D "image" 
    # ---------------
    
    # I use imshow which only work with linear axes.
    # We will have to change the axes later...
    
    axis_lim=[xmin, xmax, ymin, ymax]
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    extent = [xmin, xmax, ymin, ymax]
    img = plt.imshow(v, extent=extent, interpolation='nearest', cmap=cm.Reds, aspect='auto')
    ax.axis(axis_lim)
    
    # Make space for the colorbar
    x2_bb_eff = (x2_bb-(cb_fraction+cb_pad)*x1_bb)/(1.0-(cb_fraction+cb_pad))
    ax.set_position([x1_bb, y1_bb, x2_bb_eff-x1_bb, y2_bb-y1_bb])
    position = ax.get_position()
    
    # Remove axis ticks so that we can put log ticks on top
    ax.set_xticks([])
    ax.set_yticks([])
    
    # Add colorbar
    cb = fig.colorbar(img,fraction=cb_fraction,pad=cb_pad)
    cb.set_label('Value [unit]')
    
    # Add logarithmic axes
    # --------------------
    
    # Empty plot on top of previous one. Only used to add log axes.
    ax = fig.add_subplot(111,frameon=False)
    ax.set_xscale('log')
    ax.set_yscale('log')
    plt.plot([])
    ax.set_position([x1_bb, y1_bb, x2_bb-x1_bb, y2_bb-y1_bb])
    
    axis_lim_log=map(lambda x: 10.**x, axis_lim)
    ax.axis(axis_lim_log)
    
    plt.grid(b=True, which='major', linewidth=1)
    plt.ylabel('Some quantity [unit]')
    plt.xlabel('Another quantity [unit]')
    
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform a multivariate test for normality on some density data
I'm trying make some stuff in jQuery using ASP.NET. But the ID from runat=server
Trying to make a make generic select control that I can dynamically add elements
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a custom :confirm message for a rails form that returns data
im trying make one replace in string from a array but this dont work
Trying to make a sliding panel where you can click next and back to
Trying to make a simple toggle menu, and I can't seem to hide/show the
I'm trying make a simple control, with a some path in it. And binding
I'm trying make one treeView with infinite subgroups. I can add my groups but

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.