Plotly line plot x-axis increment by 5 - python

I have a list of times I want to use with plotly to make a line graph. The Y values work perfectly, but the x's are just spaced out by 1. I would like to make them be spaced out by 5 instead, but I don't know how to do that. I saw here that you can use a range, but that range seems to be whole numbers and an interval of 1, so how do I get the x to increase by 5 for every Y value? I can't put a for loop here to do that, so I'm not sure how this would work. Any help? Thanks!

I'm not clear on what you want to achieve, but if you only want a specific spacing between x axis labels you can use tick0 and dtick for that purpose:
xaxis=dict(
tick0=0,
dtick=5,
...
),
it would help if you show what you have and what you want for example.

Related

Reducing xtick interval [Non linear data. Floats]

I have been working on a dataframe plot, where I have 2k values to show and its indexes. However, when I try to plot them, obviously, matplotlib tries to squeeze all the index labels in the axis, making it impossible to read. It looks like this:
I have tried to increase figsize, but there are just too much xticks. Since the data is not linear (there are maybe 900 values between 0 and 1, 600 values between 1-100 and the rest >100), I cannot just re-arange the xticks with np.arange(), since the data loses correlation (there are 2k bars, but the last index is 1534.34). If I try xticks=np.arange(0, max1, max1/20) to get 20 ticks evenly spaced, I get this result:
Which obviously, is not true, since like I said, the max index is 1534.34, so that the last tick should be at the very end of the horizontal axis. Also, as as said, the first 900 values are between 0 and 1, and this is not true in the plot.
I don't know if I've been clear enough or this question is adequate, but this is my first question and I have tried to. So don't be too harsh please. However all criticism is welcomed. Thanks.
I use ax.xaxis.set_major_locator(ticker.MaxNLocator(interval)) to set my ticks on the x axis after defining the interval as some integer. See documentation here. I think this relies on calling the graph in a form such as:
fig = plt.figure(figsize = (5,2))
ax = fig.add_axes([0, 0, 1, 1])

How set a specific range of x-axis and increase the length of the plot?

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.

Y-axis is very crowded in matplotlib,How do I hide y-axis values?

I want to plot 1000 values on y-axis and 1000 values on x-axis,but my y-axis gets very crowded and I can't see any number clearly.How do I resolve this?
Note: I want to plot all 1000 values but want to display every tenth y-axis value on the y-axis.
Graph I a referring to,You can see the crowded y-axis
The function you are looking for is:
yticks()
You might want to pass a numpy array to yticks, between the values you want shown.
Check out this documentation for matplotlib.pyplot.yticks():
https://matplotlib.org/3.2.0/api/_as_gen/matplotlib.pyplot.yticks.html#matplotlib.pyplot.yticks

How to switch direction of axis in 3D scatterplot plotly?

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])

changing the xticks on a python plot

I think I'm doing something silly but I just can't see it.
In Python, I have an imshow plot, and I want to change the values on the x axis.
plt.xticks=(np.arange(grid_resid.shape[1]),xp)
plt.imshow(grid_resid[:,:], aspect="auto")
plt.show()
grid_resid.shape[1] is 1830 (0 - 1829) and the default x axis on the plot runs from 0 - 1829.
I want to change the xticks to the values of the list 'xp'. Which looks something like:
[ 53293.103161 53294.103161 53295.103161 ..., 55120.103161 55121.103161 55122.103161]
and which also has 1830 components.
My xticks however are still running from 0-1829. Could somebody point out why this is please?
xticks is a function, not a variable, so you don't want to assign to it. Also, you need to run it after imshow:
plt.imshow(grid_resid[:,:], aspect="auto")
plt.xticks(np.arange(grid_resid.shape[1]),xp)
plt.show()
Note that as your labels look long, you might want to use the rotation= argument to xticks in order to rotate the labels.

Categories

Resources