I plotted the following using plotly and got the resulting plot shown before. X is the # of hours in a day, Y is a proportion between 0-1, and Z is a categorical variable with levels {0,1,2}.
However, it's unclear why the X seems to be going the opposite direction of what we're used to with a 3D Cartesian place where it's down(-) and up(+), left(-) and right(+), and front(-) and back(+). However, X seems to decrease from front to back instead of increase. I am new to plotly and am not sure how to flip the axis so it goes from 0 to 1 instead of 1 to 0. I would greatly appreciate help on this!
fig = px.scatter_3d(X_combined, x='x', y='y', z='z',
color='set', symbol='predictions', opacity=0.7)
fig.update_traces(marker=dict(size=12,
line=dict(width=5,
color='Black')),
selector=dict(mode='markers'))
For 3D plots, the options for axes are under layout.scene.
The autorange option is therefore located under layout.scene.xaxis.autorange and can be modified like this:
fig.update_scenes(xaxis_autorange="reversed")
References:
python/3d-axes
python/layout-scene-xaxis-autorange
This should do the trick:
fig.update_xaxes(autorange="reversed")
Alternatively, you can reverse it with a specific range:
fig.update_xaxes(range=[9, 3])
Related
Hi,
How can I increase the "x" axis spread (by the appropriate python code) of the scatter plot so that each region label (Europe, S.Asia etc. etc.) will not over lap with each other as shown.
Please advice.
Have been struggling with it for quite some while.
Thanks
Consider rotating the labels with:
plt.xticks(rotation=90)
or increase figure size using:
fig = plt.scatter(...)
fig.set_size_inches(18.5, 10.5)
I´m looking to add a specific range of values to the x-axis of my plot and increase the length of this axis.
I change the range of the values of my x-axis; however, the values keep in a specific range.
Besides, I tried to increase the length of the x-axis but I failed again.
For now, I´m only plotting an empty graph, because a need to set the specifications for the axis.
Here is part of the code to the plot:
fig1, ax = plt.subplots()
ax.set_xlim(1, 1200)
ax.set_ylim(-800, 200)
ax.set_box_aspect(1)
plt.show()
This code gives me a plot square with the range of the:
x-axis = 0-200-400...1200,
I´m looking for:
x-axis = 0-50-100-150...1200
Also, I need to change the shape of the plot: square to a rectangular, where the x-axis increases the length.
Any suggestion or comment is welcome!
Thank!
plt.figure(figsize=(15,2))
Use this at first line to set the size of your plot. As you want to increase x-axis, then see that x>y in figsize parameter.
l1=np.arange(0,1250,50)
plt.xticks(l1)
Use the above code after setting y limits to set the xticks in range of 0-1200 with gap of 50.
``
You can change the size (and therefore the shape) of a pyplot figure like this:
fig1.set_size_inches(10, 8)
As for the ticks on the axis, this post gives a pretty in-depth answer on how to customize those.
I have the following plot done in Plotly
As you can see the X,Y axis are in the traditional way.
How can I rotate the axis so that X is plot vertically and Y horizontally (to the left)?
(also I would like to modify the reach and separations of each axis)
I suppose it has to do something with the layout element.
My code if so far
layout= go.Layout(title=go.layout.Title(text=title,x=0.5),
xaxis={'title':'x[m]'},
yaxis={'title':'y[m]'})
point_plot=[
go.Scatter(x=[series[listoflabels[0]]],y=[series[listoflabels[1]]],name="V0"),
go.Scatter(x=[series[listoflabels[2]]],y=[series[listoflabels[3]]],name="GT"),
go.Scatter(x=[0],y=[0],name="egoCar")
]
return go.Figure(data=point_plot, layout=layout)
EDIT:
In order to make it reproducible I modified the code to
layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5),
xaxis={'title':'x[m]'},
yaxis={'title':'y[m]'})
point_plot=[
go.Scatter(x=[3],y=[1],name="V0"),
go.Scatter(x=[5],y=[2],name="GT"),
go.Scatter(x=[0],y=[0],name="egoCar")
]
go.Figure(data=point_plot, layout=layout1).show()
and the plot is
I am not aware that Plotly has any built-in method to switch x- and y-axes. However, you can achieve this yourself but switching the titles of the x- and y-axes, then switching the parameters x and y that correspond to your points.
Then you can place the y-axis (containing your x-coordinates) on the right side in your layout, and reverse the range of the x-axis (containing your y-coordinates) so that the origin is in the lower-right side.
import plotly.graph_objects as go
# switch the axes titles
layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5),
xaxis={'title':'y[m]'},
yaxis={'title':'x[m]', 'side':'right'})
# switch the x- and y-coordinates
point_plot=[
go.Scatter(y=[3],x=[1],name="V0"),
go.Scatter(y=[5],x=[2],name="GT"),
go.Scatter(y=[0],x=[0],name="egoCar")
]
fig = go.Figure(data=point_plot, layout=layout1)
# reverse the range of the xaxis (which contains the y values)
fig.update_xaxes(autorange="reversed")
fig.show()
Derek O hit on what is the easiest to implement. It's the graph analogy of 1km is 0.6 miles or a, b = b, a
A second option is to abstract the data and your axes. That's essentially saying the same thing but can be easier to read as Plotly has a unique ability to turn clear, well-structured Python into ugly JS without parenthesis pretty quickly.
Option three is to further abstract this and have it set your axes for you.
I am creating a plot based on a DataFrame:
cg = sns.clustermap(df_correlations.T)
The problem is that the x and y axis have unwanted labels in it which come from a hierarchical index. Thus I want to try and remove those labels e.g. like this:
ax = cg.fig.gca()
ax.set_xlabel('')
ax.set_ylabel('')
But this has no effect. How can I remove the labels on the x and y axis?
Without a mcve of the issue it's hard to know where the labels come from (I don't know how the dataframe needs to look like such that labels are produced, because by default there should not be any labels.) However, the labels can be set - and therefore also set to an empty string - using the known methods .set_xlabel and .set_ylabel of the heatmap axes of the cluster grid.
So if g is a ClusterGrid instance,
g = sns.clustermap(...)
you can get the heatmap axes via
ax = g.ax_heatmap
and then use any method you like to manipulate this matplotlib axes.
ax.set_xlabel("My Label")
ax.set_ylabel("")
Turn off xticklabel, and yticklabel will address your problem.
sns.clustermap(df,yticklabels=False,xticklabels=False)
try plt.axis('off'), it may solve your problem.
I would like to draw a standard 2D line graph with pylot, but force the axes' values to be between 0 and 600 on the x, and 10k and 20k on the y. Let me go with an example...
import pylab as p
p.title(save_file)
p.axis([0.0,600.0,1000000.0,2000000.0])
#define keys and items elsewhere..
p.plot(keys,items)
p.savefig(save_file, dpi=100)
However, the axes still adjust to the size of the data. I'm interpreting the effect of p.axis to be setting what the max and min could be, not enforcing them to be the max or min. The same happens when I try to use p.xlim() etc.
Any thoughts?
Thanks.
Calling p.plot after setting the limits is why it is rescaling. You are correct in that turning autoscaling off will get the right answer, but so will calling xlim() or ylim() after your plot command.
I use this quite a lot to invert the x axis, I work in astronomy and we use a magnitude system which is backwards (ie. brighter stars have a smaller magnitude) so I usually swap the limits with
lims = xlim()
xlim([lims[1], lims[0]])
To answer my own question, the trick is to turn auto scaling off...
p.axis([0.0,600.0, 10000.0,20000.0])
ax = p.gca()
ax.set_autoscale_on(False)
I tried all of those above answers, and I then summarized a pipeline of how to draw the fixed-axes image. It applied both to show function and savefig function.
before you plot:
fig = pylab.figure()
ax = fig.gca()
ax.set_autoscale_on(False)
This is to request an ax which is subplot(1,1,1).
During the plot:
ax.plot('You plot argument') # Put inside your argument, like ax.plot(x,y,label='test')
ax.axis('The list of range') # Put in side your range [xmin,xmax,ymin,ymax], like ax.axis([-5,5,-5,200])
After the plot:
To show the image :
fig.show()
To save the figure :
fig.savefig('the name of your figure')
I find out that put axis at the front of the code won't work even though I have set autoscale_on to False.
I used this code to create a series of animation. And below is the example of combing multiple fixed axes images into an animation.
Try putting the call to axis after all plotting commands.