Matplotlib: move Origin to upper left corner
Asked Answered
D

1

5

In Matlab, the plot is to be made such that origin is on top-left corner, x-axis is positive south to the origin, y-axis is positive east to the origin; x-axis is numbered on the left margin and y-axis is labelled on the top margin.

figure();
set(gca,'YAxisLocation','Right','YDir','reverse')
axis([0 10 0 10]);
daspect([1,1,1])
grid on
view([-90 -90])

How to achieve the same in Python? I proceeded like:

import matplotlib.pyplot as plt
plt.figure(1)

plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

What is the python equivalent for:

  1. set(gca,'YAxisLocation','Right','YDir','reverse')
  2. daspect([1,1,1])
  3. view([-90 -90])

EDIT:

figure
set(gca,'YAxisLocation','right','XTick',0:15,'YDir','reverse')
axis([0 16 0 16]);
daspect([1,1,1])
hold on
text(2,8,'CHN');
text(8,2,'USA');
view([-90 -90])

Output is:

enter image description here

Daly answered 12/3, 2015 at 14:37 Comment(0)
S
10

Here is an commented Example that shows how you can invert the axis and move the ticks to the top, set the grid state and imitate the view() command:

import matplotlib.pyplot as plt
import numpy as np

plt.figure()    
plt.axis([0, 16, 0, 16])     
plt.grid(False)                         # set the grid
data= [(8,2,'USA'), (2,8,'CHN')]

for obj in data:
    plt.text(obj[1],obj[0],obj[2])      # change x,y as there is no view() in mpl


ax=plt.gca()                            # get the axis
ax.set_ylim(ax.get_ylim()[::-1])        # invert the axis
ax.xaxis.tick_top()                     # and move the X-Axis      
ax.yaxis.set_ticks(np.arange(0, 16, 1)) # set y-ticks
ax.yaxis.tick_left()                    # remove right y-Ticks

Image: enter image description here

Serried answered 12/3, 2015 at 15:16 Comment(6)
I am using matplotlib rather than pylab. Could you give your answer in terms of matplotlib or is it same?Daly
Yup, that's great. Also i want to view the graph in a special way (i want to view the graph from below the x-y plane generated by your code, with my head in west direction and foot in east), as i said in my question. In Matlab, i know that view([-90 -90]) does the trick. What to do in Python?Daly
Could you show me a sample plot with data before and after the view transformation? I can´t quiet get how it should look. In general you can take the plt.axis( [xmin, xmax, ymin, ymax] ) command to change the viewport. For Example: Just exchange xmin and xmax for a flip around the y-Axis .Serried
Sorry for getting back to you late. So i have put a short Matlab code. Your python code also gives almost identical result but could you modify your code to give the exact output as the Matlab code in Edit section?Daly
I updated my answer. Unfortunatly there is nothing like the view() command afaik. You can imitate that by switching your data though.Serried
You can invert the yaxis with ax.invert_yaxis().Quathlamba

© 2022 - 2024 — McMap. All rights reserved.