Matplotlib: Secondary axis shares values with both of the main axes - python

I have added a secondary x-axis to my plot. The aim is just to provide another snippet of information to the data shown by the primary x and y axes. As you can see from the attached image, I have used twiny() command to share the y-axis and get_shared_x_axes().join(ax1,ax2) to align the secondary axis with the primary x-axis.
However, this results in plotting duplicate data. What I really need is in fact for the rectangular markers to provide another scale of information given by the circular markers in the attached image. Matplotlib examples handle this situation by writing a function for instance to convert Celsius degrees to Fahrenheit. In my case, there is no apparent relationship, just scatter data points. Could you please point me towards to the right direction? Many thanks!

Related

Manually specify shape of plotly violin or similar trace when using categorical axes

I am aiming to combine two types of charts - a horizontal bar chart using a categorical axis and some form of line, scatter, violin, or similar chart that displays a trace paired with each category on the same axes but "dithers" up and down around some bounds.
Below is sort of what I'm trying to create. However, the shape of the violin chart is generated automatically behind the scenes from scattered points. In my use case, I already have what the distributions are and I'm trying to plot them over top of related data.
I can't find a utility in Plotly that allows me to specify a categorical axis, but then offset points, lines, etc from that category-derived position on the axis.

Get boxplot rectangles coordinates in plotly

I need to draw additional graphics on top of plotly go.Box traces, therefore I need to know X and Y coordinates for boxplot rectangle vertices. So far the only solution I came up with is basically recalculating everything (quartiles; X positions based on boxgap, boxgroupgap, etc.), then manually setting the y-axis range to know where everything will end up on the plot. This seems very cumbersome.
Is there a way in python to get the coordinates of go.Box boxplot elements, especially the grouped boxplots with categorical x-axis? As far as I understand these coordinates are calculated in JS frontend -- maybe there is some trick to get them back with Dash using callbacks?

How to add box plots on top of scatter plot

I want to plot boxplots on top of the scattered points like this.
I know I have to bin the data into intervals first but I couldn't find the function that does all of this. Sample x and y data are saved here as .npy.
I would look into using matplotlib. Boxes can be drawn as such:
https://matplotlib.org/gallery/pyplots/boxplot_demo_pyplot.html?highlight=boxplot
and scatter plots can also be drawn as such: https://matplotlib.org/gallery/lines_bars_and_markers/scatter_demo2.html?highlight=scatter
There is a search functionality on their site, along with plenty of documentation on how to utilize their library.
As for your specific question, you can specify zorder when drawing many of the things in matplotlib, and you could use that to define your boxplots to be on top. I believe if no zorder is defined that it draws items in the order they are encountered in your program (so you could draw scatter plots and then box plots and they should appear correctly as in your diagram above!

How to make data points in a 3D python scatter plot look like "discs" instead of "spheres"

In a standard 3D python plot, each data point is, by default, represented as a sphere in 3D. For the data I'm plotting, the z-axis is very sensitive, while the x and y axes are very general, so is there a way to make each point on the scatter plot spread out over the x and y direction as it normally would with, for example, s=500, but not spread at all along the z-axis? Ideally this would look like a set of stacked discs, rather than overlapping spheres.
Any ideas? I'm relatively new to python and I don't know if there's a way to make custom data points like this with a scatter plot.
I actually was able to do this using the matplotlib.patches library, creating a patch for every data point, and then making it whatever shape I wanted with the help of mpl_toolkits.mplot3d.art3d.
You might look for something called "jittering". Take a look at
Matplotlib: avoiding overlapping datapoints in a "scatter/dot/beeswarm" plot
It works by adding random noise to your data.
Another way might be to reduce the variance of the data on your z-axis (e.g. applying a log-function) or adjusting the scale. You could do that with ax.set_zscale("log"). It is documented here http://matplotlib.org/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.set_zscale

On adjusting margins in matplotlib

I am trying to minimize margins around a 1X2 figure, a figure which are two stacked subplots. I searched a lot and came up with commands like:
self.figure.subplots_adjust(left=0.01, bottom=0.01, top=0.99, right=0.99)
Which leaves a large gap on top and between the subplots. Playing with these parameters, much less understanding them was tough (things like ValueError: bottom cannot be >= top)
My questions :
What is the command to completely minimize the margins?
What do these numbers mean, and what coordinate system does this follow (the non-standard percent thing and origin point of this coordinate system)? What are the special rules on top of this coordinate system?
Where is the exact point this command needs to be called? From experiment, I figured out it works after you create subplots. What if you need to call it repeatedly after you resize a window and need to resize the figure to fit inside?
What are the other methods of adjusting layouts, especially for a single subplot?
They're in figure coordinates: http://matplotlib.sourceforge.net/users/transforms_tutorial.html
To remove gaps between subplots, use the wspace and hspace keywords to subplots_adjust.
If you want to have things adjusted automatically, have a look at tight_layout
Gridspec: http://matplotlib.sourceforge.net/users/gridspec.html

Categories