userxigement
userxigement

Reputation: 97

Matplotlib: move Origin to upper left corner

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

Upvotes: 5

Views: 19289

Answers (1)

Mailerdaimon
Mailerdaimon

Reputation: 6090

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

Upvotes: 10

Related Questions