Ram
Ram

Reputation: 11

Reversing the axis range in 3D graph in python

I am new to python , so help me to overcome this problem.

I have plotted a 3D graph using some random points. After plotting i got a graph But to get the desired graph , i need to reverse the Y axis. I did it using

gg.scatter(Ys1,Xs1,Zs1)

gg = plt.gca()

del Ys1[:],Xs1[:],Zs1[:]

gg.set_xlabel(' Y Label')
gg.set_ylabel(' X Label')
gg.set_zlabel(' Z Label')

plt.gca()invert_yaxis()

My graph is reversed but unfortunately am not getting the axis range displayed in my plot. If i don reverse , am getting them displayed.

How do i get my axis range displayed.

Sorry am not able to post my graphs since don't have reputation above 10.

I'll be glad if this is solved. Thank you.

Upvotes: 0

Views: 1678

Answers (1)

Greg
Greg

Reputation: 12234

Ram please consider the comments made above as they will help you get better responses. I have attempted to interpret your code and give a working solution.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# Create data so anyone can run the script
n = 1000

Xs1 = np.random.randint(0,3,n)
Ys1 = np.linspace(0,10,n)
Zs1 = np.exp(Ys1)


# This needs to be BEFORE gg.scatter otherwise it is nonsense
gg = plt.gca(projection="3d")

# This is how I would invert a list
Ys1_inverted = [Ys1[n-1-i] for i in range(n)]

# One can plot either Ys1 or Ys1 inverted here (see images below)
gg.scatter(Xs1,Ys1,Zs1)

gg.set_xlabel(' Y Label')
gg.set_ylabel(' X Label')
gg.set_zlabel(' Z Label')

plt.show()

Using the two different lists Ys1 and Ys1_inverted in the plot command gives the following images:

Using Ys1 data

Ys1_inverted Using Ys1_inverted data

This method displays the correct ranges for all axis.

Upvotes: 1

Related Questions