How to invert scatter plot axis - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to invert scatter plot axis (/thread-34967.html) |
How to invert scatter plot axis - Mark17 - Sep-20-2021 Hi all, Here's some code: import pandas as pd import matplotlib.pyplot as plt import time import numpy as np start_time = time.time() df = pd.DataFrame(columns=['DTE', 'Long Delta', 'Short Delta', 'Delta Spread']) # Checking all permutations covered by program: for dte in range(30,61): long_delta = np.arange(-0.1,-0.35,-0.05) short_delta = np.arange(-0.25,-0.50,-0.05) if dte == 30: for ld in long_delta: for sd in short_delta: plt.scatter(ld,sd) plt.tight_layout() #to prevent axis labels from being cut off in saved figure plt.xlabel('Long Delta') plt.ylabel('Short Delta') end_time = time.time() print('Elapsed time is {:.0f}ms'.format(1000*(end_time-start_time)))How do I reverse the axes on the graph so they go from largest to smallest (left to right and down to up)? Mark RE: How to invert scatter plot axis - jefsummers - Sep-20-2021 import matplotlib.pyplot as plt ex = [0,0.2,0.5,1] ey = [0.1, 1,2.5,4] #plot normal plaid = plt.scatter(ex, ey) #now reverse axes fig, ax = plt.subplots() ax.scatter(ex, ey) ax.set_xlim(1,0) ax.set_ylim(5,-1) ax.grid = True pltYou might use plt.show() - I was in a notebook so just used plt RE: How to invert scatter plot axis - Mark17 - Sep-22-2021 (Sep-20-2021, 06:52 PM)jefsummers Wrote:import matplotlib.pyplot as plt ex = [0,0.2,0.5,1] ey = [0.1, 1,2.5,4] #plot normal plaid = plt.scatter(ex, ey) #now reverse axes fig, ax = plt.subplots() ax.scatter(ex, ey) ax.set_xlim(1,0) ax.set_ylim(5,-1) ax.grid = True pltYou might use plt.show() - I was in a notebook so just used plt So I have to use the object-oriented approach with fig and ax to invert axes? Also, when I initially did this I didn't know what the min and max were going to be: matpltlib calculated that on its own. Do I have to run once to see what the min and max are and then take those values to build into the code? Thanks jefsummers! Mark RE: How to invert scatter plot axis - jefsummers - Sep-22-2021 I believe that is the way to get access to the axes. Change lines 11 and 12 to ax.set_xlim(max(ex), min(ex)) ax.set_ylim(max(ey),min(ey)) |