Python Forum
How to invert scatter plot axis
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invert scatter plot axis
#1
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
Reply
#2
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
plt
You might use plt.show() - I was in a notebook so just used plt
Mark17 likes this post
Reply
#3
(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
plt
You 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
Reply
#4
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))
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked plt.scatter() errors asja2010 0 1,412 Oct-13-2022, 08:15 AM
Last Post: asja2010
  Making a plot with secondary y-axis bigger snkm 0 1,096 Feb-10-2022, 09:40 AM
Last Post: snkm
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Animated scatter plot maximan 0 1,616 Jan-18-2021, 03:53 PM
Last Post: maximan
  How to plot intraday data of several days in one plot mistermister 3 2,854 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,564 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,426 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Group scatter plots Mekala 0 1,618 Jul-23-2020, 02:18 PM
Last Post: Mekala
  Invert Pillow image colours chesschaser 5 3,597 Jul-11-2020, 02:59 PM
Last Post: chesschaser
  how to nest loop for 4*4 scatter plot kassamohammed 0 2,544 Jun-23-2020, 09:47 AM
Last Post: kassamohammed

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020