X Tutup
Skip to content

Incorrect transData in a simple plot #7518

@ondrejdee

Description

@ondrejdee
  • matplotlib 1.5.3 (pip install)

  • python 2.7.6 (apt-get from ubuntu repository)

  • ubuntu 14.04

  • goal: compute the data coordinates of upper right corner of axes.

  • issue: the code below produces wrong answer p_data_coords = [3.0, 3.0].
    But, when the last line of code is called again from the interactive session of ipython (thus, some graphics updates probably take place), correct answer is produced: p_data_coords = [5.375, 3.]

  • copies of transAxes and transData returned from ax_coords_to_data_coords indicate that it is transData which changes between the calls.

import numpy as np 
import matplotlib.pyplot as plt 

def show_some_data(): 
    fig = plt.figure(figsize = (8, 3))
    plt.plot(3*np.random.rand(100), 3*np.random.rand(100), 'o') 
    plt.axis('equal') 
    plt.xlim(0, 3) 
    plt.ylim(0, 3)
    plt.show() 
    ax = plt.gca() 
    return fig, ax 

def ax_coords_to_data_coords(ax, point): 
    t = ax.transAxes + ax.transData.inverted()
    return t.transform(point), ax.transAxes.frozen(), ax.transData.frozen()

fig, ax = show_some_data() 

# upper right corner of coordinates: 
p_ax_coords = (1, 1) 
# get its coordinates in data coord system: 
p_data_coords, t_ax, t_data = ax_coords_to_data_coords(ax, p_ax_coords) 

ax_bounds

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup