Truncated figure with plotly - python

I am facing a problem with the Scatter3d from plotly: the figure is always truncated at the bottom:
I create the plot via plotly.express this way:
fig = px.scatter_3d(BFM_pcaFull, x=0, y=1, z=2, color=3)
with BFM_pcaFull being the pandas.DataFrame where the data are stored. I tried to create the plot via plotly.graph_object instead of plotly.epxress but the result is the same.
I tried to tweak the layout parameter via the update_layout() method of fig:
Padding
Auto margin
Scaling
scaleratio
constrain
of course without any change to the graph (which does surprise me and make me think I am doing something wrong, even if apparently the 3D surface seems to follow different rules somewhat).
An issue for the same problem is open on the Github repo of the project but has not been solved so far (https://github.com/plotly/plotly.py/issues/3785).
Has anybody faced the same problem and found a solution by any chance?
Thanks for your help

To avoid missing parts of the graph in a 3D graph, you can change the viewpoint angle. See here for more information. The following code can be used to deal with this problem.
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length',
y='sepal_width', z='petal_width',
color='species')
fig.show()
When the camera viewpoint is changed
fig.update_layout(margin=dict(l=0,r=0,t=0,b=0), scene_camera=dict(eye=dict(x=2.0, y=2.0, z=0.75)))

Related

Add an image to a plotly express figure

I'm new to plotly/plotly express and i'm having a really hard time finding any working example other than the documentation examples for the library (which are really basic and standard).
I have, let's say a scatter plot, in plotly express:
fig = px.scatter(any_random_data)
And i want to add to that plot an image in a fixed (x,y) position, but i don't know (and can't find!) if there is any kind of method for that.
I've seen there is an add_trace() method to add traces to the plot (i guess), is there any similar function for adding images? (Like add_image() or something)

inset_axes polar projection in a polar plot in matplotlib

I would like to create a polar plot with a polar zoom.
This is what I have done do far:
fig = plt.figure(figsize=(30, 15))
G = gridspec.GridSpec(1, 2)
axes_1 = plt.subplot(G[0],projection='polar')
axes_1.inset_axes(bounds=[0.0,0.0,0.1,0.1],projection='polar')
axes_2 = plt.subplot(G[1])
and here the error that I get:
'Axes' object has no property 'projection'
This is quite strange, because in the manual of inset_axes I have found the option projection (here).
There are some attempts here, but for me is totally unclear, or here, but this last it is not what I what. It is just another plot.
Thanks
From version 3.6, matplotlib.axes.Axes.inset_axes accepts "polar" and "projection" parameters. If you use a older version, there is also a workaround using mpl_toolkits.axes_grid1.inset_locator.inset_axes() (see this answer).
Creating a rectangular zoom of a polar plot is not as straightforward, but this answer may help you.

Writing Data Point Name Just Above the Data Point in Python Plotly

I have a dataset and I plotted using plotly python
The name of the models is shown as a legend on the right side which makes it a little difficult to interpret. Is there any way to write the model name just above the color circle represent on the graph similar to what shown below? Something similar in mathplotlib will also help me.
You need to add labels in you scatter statement.
import plotly.express as px
#df data
fig = px.scatter(df, x="latency", y="AP", text="type", size_max=60)
fig.update_traces(textposition='top center')

Seaborn: edges in distplot don't fit the plot

When using matplotlib and seaborn in Jupyter with Python 2.7.12, I noticed that the edges of the distplot I drew don't fit the plot correctly (cf. the 2 figures below). At first I thought it was an issue with the code, but when trying the same code on someone else's laptop with the exact same versions of Jupyter and Python, the issue did not occur. Could anyone point me in the right direction?
wrong plot:
right plot:
I would gladly share the notebook with the code and the dataset, but since I am kind of new to sharing notebooks online, I do not know what the 'standard way to go' is.
Any help would be greatly appreciated.
It looks to me like the difference between the two plots is the bandwidth of the kernel used to calculate the KDE. Maybe there are different default values on both machines.
Try to play with either the bw= or kernel= parameters (documentation). Like so:
fig,(ax1,ax2) = plt.subplots(2,1, figsize=(5,10))
x = np.random.randn(100)
sns.distplot(x, ax=ax1)
sns.distplot(x, kde_kws={'bw':5}, ax=ax2)

Sloppy SVG generated by matplotlib resulting on poor clipping of datapoint drawing in figures

Figures that I create with matplotlib do not properly clip points to the figure axes when rendered, but instead draw additional points, even though such figures look fine in some viewers.
For example (following an example from the documentation) using
import matplotlib
matplotlib.use('SVG')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig = plt.figure()
ax = fig.add_subplot(111)
x, y = 12*np.random.rand(2, 1000)
ax.set(xlim=[2,10])
ax.plot(x, y, 'go') # plot some data in data coordinates
circ = patches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes,
facecolor='yellow', alpha=0.5)
ax.add_patch(circ)
plt.savefig()
I seem, when viewed for example in OS X Preview, to get
but when I view it in other editors, such as iDraw I get a mess (where, weirdly, there is a combination of correct clipping of edge points, failed clipping of points outside the axes, and clipping of the canvass at a point that does not correspond to either the axes or the range of data):
I'm not experienced with SVG, but those I've asked tell me that
I looked at the SVG file and didn't like what I saw. Characters are
flattened, and definition sections are scattered throughout the file
instead of being at the top; some defs are inside graphics constructs.
There's a lot of cruft. It turns out the definition of the clip-path
is at the very end of the svg file -- after all the uses ...
How can I get matplotlob to generate SVG that does not have these issues? I know that I can edit the SVG, but I have no idea how, and doing so defeats the purpose and I hope that it is not necessary to add a "by hand" step to my workflow.
I'm interested in understanding what the cause of the sloppy SVG generated by matplotlib is: whether it's something that can be avoided by coding a bit differently (though not, clearly, by simply checking whether every data point is in range), or whether it's a bug in matplotlib (or perhaps whether it's just a problem with ambiguities in the SVG standard). The goal is getting matplotlob to generate SVG that is not buggy.
This is probably related to a know issue and also comes up in pdfs (matplotlib data accessible outside of xlim range)
See Issues #2488 and #2423 (the later which includes a proposed fix for pdf). It is milestoned for 1.4.

Categories