the axes control of mlab.axes - python

This is just a small question. I use the sentence below to control the three axes ranges.
mlab.axes(xlabel='x', ylabel='y', zlabel='z',ranges=(0,10000,0,10000,0,22),nb_labels=10)
In fact the real data ranges are (3000,4000),(5000,6000),(0,22) respectively.
However the axes of the figure I plot is scaled to (0,10000,0,10000,0,22).
I did not find a parameter of mlab.axes can control that.
Do I have to calculate the data ranges every time? Without knowing the real data range, is there a way to make the axis range obey the real data?

Related

How do I make the yxaes of my python plot to start from bigger and ending to smaller numbers?

I want to change the y axes in my python scatter plot , to start from big values and end to small of my data. How can I do this without setting specific ylim() in order to have a more generalized code?

grid of subplots with overarching axes in python (data with 3 independent variables)

What I am trying to do, is visualize data with 3 independent variables. So I made a grid of plots using subplots, where the x axis is the same everywhere. The y axis, on the other hand, depends on the position on the grid. In the x-direction you have another independent variable and the same in the y-direction. So what I would like to do, is to have some kind of overarching axis where the ticks and the numbers are right underneath (for the axis below) each column, so you know which value of this particular variable the column corresponds to. I can't seem to find any info on how to do this with matplotlib.
fig,ax=plt.subplots(5,3,sharex='col',sharey='row',figsize=(6,10))
for inrs in range(nrs):
for inrg in range(nrg):
n=int(neti[inrs,inrg])
ax[inrs,inrg].plot(xet[inrs,inrg,1:n],dphid[inrs,inrg,1:n],color='green')
plt.show()
I found a way to do this. I don't think there is any "regular" way of doing this. What I did was to use the command
ax=fig.add_axes([0.25,0.05,0.55,0.000001])
for the x axis. Here, (0.25,0.05) is the position (x0,y0), and (0.55,0.000001) is the (width,height) of the figure. The exact numbers depend on the plots you have. What you are doing in essence, is creating an additional very thin plot, where only the x-axis will be visible. To disable the y-axis:
ax.axes.get_yaxis().set_visible(False)
Then you can use the commands:
ax.set_xlabel("xlabel")
ax.set_xlim([])
ax.set_xticks([])
to complete the axis. For the y-axis, it's the same thing. In my case, for the x-axis, the ticks are not evenly spaced. To fix this, I simply created another such plot within the previously existing one to put a particular tick where I needed it to be.

Plotly autorange axes setting

I have one question for plotly x and y axes setting.
It's possible to autorange the axis only the first time based on all input data and then turn off rescaling while manipulating the data input (in the legend)?
https://plot.ly/python/reference/#layout-xaxis-type
From documentation: 'autorange' default: True
Determines whether or not the range of this axis is computed in relation to the input data.
I need autorange only to be done first time and then it should behave like False. I need it so I can make evaluations relative to the whole dataset.
Maybe it can be done another way, not by manipulating autorange but that's why I'm asking.
MY EXAMPLE:
Imagine you have visualization like this. I have labeled many groups so that I can turn them off/on by plotly functionality. But the problem for me is that it is rescaling everytime based on the input data (only the ones which are 'turned on').
This is after I isolate the GROUP 1. But I want the same x-axis and y-axis as I had before (which was 'autoranged' when I started the visualization).
Thanks for your help!

Trim data outside 3d plot in matplotlib

I have a set of PDF that I need to plot for a certain section of the PDF domain. However, when I plot my lines on a 3d plot I get tails for each PDF,
Is there a clean way to not plot the tails that happen outside my plot limits? I know I can change the data to NaNs to achieve the same effect but I want to do this in matplotlib. Here is my current workaround code,
`# trim the data
y = np.ones(PDF_x.shape)*PDF_x
y[y>95]= np.nan
y[y<75]= np.nan
# plot the data
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(PDF_capacity.shape[1]):
ax.plot(life[i]*np.ones((PDF_x.shape)),y,PDF_capacity[:,i], label='parametric curve')
# set the axis limits
ax.set_ylim(75,95)
# add axis labels
ax.set_xlabel('charge cycles to failure point of 75% capacity')
ax.set_ylabel('capacity at 100 charge cycles')
ax.set_zlabel('probability')`
After trimming I can make the following plot,
Masking the data with nan in the way you're doing it is a good and practical solution.
Since matplotlib 3D plots are projections into 2D space, it would be hard to implement automatic clipping. While I do think it would be possible, I'm not convinced that it's worth the effort. First, because you would need to treat different kinds of plots differently, second, because at least in some cases it would probably turn out that masking the data is still the best choice. Now, doing a complex subclassing of the plotting objects just to do the same thing that can be manually done in one or two lines is probably overkill.
My clear recommendation would therefore be to use the solution you already have. Especially since it does not seem to have any drawbacks so far.

Scatter plot and contour plot with same colors

Is it possible to force plt.scatter into the same color levels as plt.contourf and plt.contour? For example, I have code that makes a plot like this:
to make the first subplot, I use
cs=m[0].scatter(xs,ys,c=obsData,cmap=plt.cm.jet)
m.colorbar(cs)
To make the second subplot, I use
cs2=m[1].contourf(x,y,areaData,cmap=cs.cmap)
And for each subsequent subplot, I use
m[ind].contourf(x,y,areaData,cmap=cs.cmap,levels=cs2.levels
where areaData is recalculated within a loop.
My question is, how can I force the first subplot to have the same colors as the other subplots? I am looking for an equivalent to the levels=cs2.levels keyword argument.
As you noted in a comment, your scatter and contour data are not directly related, but you want to display them on the same colormap.
I suggest setting a common colour span that contains both sets of data. Since obsData refers to the scatter points and areaData to the contours, I'd set
vmin,vmax = (fun(np.concatenate([obsData,areaData])) for fun in (np.min,np.max))
to determine the span of the collected data set (obviously, to be generalized for multiple input data sets). These can be passed to scatter and contourf to set the limits of the colour mapping:
cs = m[0].scatter(xs,ys,c=obsData,cmap=plt.cm.viridis,vmin=vmin,vmax=vmax)
cs2 = m[1].contourf(x,y,areaData,cmap=cs.cmap,vmin=vmin,vmax=vmax)
Some manual increase of the span might be in order to obtain a pretty result.
Note that I changed the colormap to viridis. If you really want to fairly represent your data, this should be your first step.

Categories

Resources