Grouped errorbar with array of strings as data points - matplotlib

I have different measurements from two+ sensors. I want to compare the performance of each sensor for each measurement with errorbars (mean and std). I have no problems creating and formatting a standard errorbar plot for one y (sensor) and yerr per data point (measurement). But i'm trying to create a plot like this:
I can neither find the option to do this in the matplotlib documentation nor when i google it or search this site. The closest i found was this thread:
matplotlib: grouping error bars for each x-axes tick
But this solution doesn't work for me since my datapoints aren't numbers but a pandas dataframe index of strings.

So i found the solution in the Matplotlib documentation after all. Here's the link for people who might have the same question:
A bar plot with errorbars and height labels on individual bars.

Related

A plot describing the density of data points in 2D space in Julia

I am trying to use Julia to create a gif animation showing the change of density of data points with time (the data points are at the beginning concentrated at the center, and than spread to the sides, a little bit like 2D Gaussian of variance increasing with time). I have checked a catalogue of available kinds of plots in Julia:
http://docs.juliaplots.org/latest/examples/gr/
And I have tried contour plot, heatmap and 2D histogram. However, it seems that the grids of a heatmap or a contour plot have to be manually specified which is highly inconvenient. A 2D histogram serves the purpose better, but it's more related to the number of data points and when I want the plot to be more continuous by setting more bins, it cannot describe the density of data points well. Are there any good substitutes of the 2D density plot in matplotlib in Julia as the following?
https://python-graph-gallery.com/85-density-plot-with-matplotlib/
You use a package like KernelDensity to calculate the point density, then plot that. Here's an example
using StatsPlots, KernelDensity
a, b = randn(10000), randn(10000)
dens = kde((a,b))
plot(dens)
The philosophy, in the Plots package and other places in Julia, is that you generate the object you are interested in first, and then dispatch takes care of plotting it correctly.
Alternatively, you can always use PyPlot to plot anything using matplotlib directly.

Holoviews: Format legend and colors of Spread and Curve Overlay

Given a tidy Pandas column with 4 or more columns, I want an otherwise very straightforward plot: two of the columns should be the x-y axes of a single figure, and one of the columns should index an Overlay of N Curve objects based on the x-y columns, and N Spread objects, using the final column as error. So if N=4 there should be 4 curves and four spreads. The curves and spreads with same index should be the same color, and the legend should attest to this.
Using table.to(hv.Curve,'col1','col2') I can get a Holomap for the curves, and with some effort I can do the same for the spread. If I then call .overlay() I get a nice figure for the curves including a legend, but when I do the same for the spread the legend vanishes. If I overlay the two, the legend likewise vanishes and the color cycle stops working, making all curves and spreads the same color. If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.
This seems like a very standard plot, but I can find very little in the Holoviews docs about pairing different Elements or controlling the legend.
This is a bit difficult to answer without any concrete code, for example I can't reproduce some of the issues you are describing. However the first issue is simply that show_legend is not enabled by default for the Spread elemen. In the case of plotting a Curve and Spread using .to and .overlay, here is what I can confirm works:
%%opts Spread [show_legend=True width=600] Overlay [legend_position='right']
df = pd.DataFrame({
'index': np.arange(100), 'y': np.random.randn(100).cumsum(),
'err': np.random.rand(100)+0.1, 'z': np.repeat(np.arange(10), 10)
})
ds = hv.Dataset(df)
ds.to(hv.Curve, 'index', 'y', 'z').overlay() * ds.to(hv.Spread, 'index', ['y', 'err']).overlay()
If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.
This is indeed a current limitation since we recommended against nesting objects in this way in the past, however I have just opened this PR which will allow this approach as well.

subplot with shared axis but different ticks and labels

I make a plot with different subplots (using gridspec.GridSpec). Two subplots share the same x-axis (sharex=ax1 in the definition of the second subplot).
However, as one subplot shows the indices of the chronologically sorted data, and the second subplot shows the corresponding decades, I want seperate ticks and labels for the x-axes of both plots. This seems not possible, a unique set of labels and ticks are assigned to both subplots. Until now, I can only:
use different x-axes and thus assign two sets of ticks and labels.
In that case, the axes are not alligned although
ax1.set_xlim([start, stop]) are similarly defined for both subplots
use a common x-axis and one set of ticks and labels
I do not find a solution for this on the internet. Is someone able to help? Thank you in advance!

Matplotlib's Figure and Axes explanation

I am really pretty new to matplotlib, though I know that it can be very powerful.
I've been reading number of tutorials and examples and it's a real hassle to understand how does matplotlib's Figure and Axes work. I am illustrating, what I am trying to understand, with the attached figure.
I know how to create a figure instance of certain size in inches. However, what bothers me is how can I create subplots and then axes, within each subplot, with relative coordinates (bottom=0,left=0,top=1,right=1) as illustrated.
So, for example I want to create a "parent" plot area (say (6in,10in)). Then, I want to create two subplot areas, each with size (3in,3in), with 1in space from the top, 2in space between the two vertical subplot areas and 1in from bottom. Then, 1in space on the left and 2in space on the write. In the same time, I would like to be able to get the coordinates of the subplot areas with respect to the main plot area.
Then, inside the first subplot area, I'd like to create 2 axis instances, with Axis 1, having coordinates with respect to Subplot Area1 (0.1,0.7,0.7,0.2) and Axes 2 (0.1,0.2,0.7,0.5). And then of course I'd like to be able to plot on these axes e.g., ax1.plot()....
If you could provide a sample code to achieve that, then I can study it.
Your help will be very much appreciated!
a subplot and an Axes object are really the same thing. There is not really a "subplot" as you describe it in matplotlib. You can just create your three Axes objects using gridspec without the need to put them in your "subplots".
There are a few different ways to create Axes instances within your figure.
fig.add_axes will create an Axes instance at the position given to it (you give it [left,bottom,width,height] in figure coordinates (i.e. 0,0 is bottom left, 1,1 is top right).
fig.add_subplot will also create an Axes instance. In this case, rather than giving it a rectangle to be created in, you give it the number of rows and columns of subplots you would like, and then the plot_number, where plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.
For example, to create the top-left Axes in a grid of 2 row and 2 columns, you could do the following:
fig.add_subplot(2,2,1)
or the shorthand
fig.add_subplot(221)
There are some more customisable ways to create Axes as well, for example gridspec and subplot2grid which allow for easy creation of many subplots of different shapes and sizes.

Discrete Scatter Plot Visualization

This is a very special plotting request, but I have data I want to view in a very particular way. Here's the situation:
1) The data I have is binned into 25 bins, each bin contains a different number of data points. The larger the bin value, the smaller then number of data points it has within it, roughly speaking (This is just a result of the data processing which was done).
[9568, 10079, 10137, 10090, 10154, 10091, 10046, 10116, 9959, 9401, 7703, 5216, 3089, 1632, 854, 466, 221, 106, 63, 27, 12, 5, 1, 0]
2) I have access to the bin values.
[ 0.02648645 0.09996368 0.1734409 0.24691813 0.32039536 0.39387258
0.46734981 0.54082703 0.61430426 0.68778148 0.76125871 0.83473593
0.90821316 0.98169038 1.05516761 1.12864483 1.20212206 1.27559928
1.34907651 1.42255373 1.49603096 1.56950818 1.64298541 1.71646264]
I can easily produce an 'errorbar' type plot in matplotlib (the y-axis is scaled from radius to degrees below):
But, this is not particularly insightful for what I'd like to study. I'd really like to know if there are 'islands' of angle values within each bin, and to do this, I would need something like a scatterplot or an imshow/hexbin type plot, where the density of points can be represented by color (in the case of imshow/hexbin at least). The following is an example of what happens when represented by a regular scatterplot with the smallest marker size:
Would anybody know of a good way to generate this type of visualization?
EDIT: This may help clarify a couple of things. The following plot is a sample of what a histogram would look like for the first couple of bins. Data contained within bins seem to follow some sort of distribution (I mentioned 'islands' before, because I am not ruling out the possibility of multiple peaks in the distribution). I would like this distribution to be visualized for all bins simultaneously. In other words, is there a way to do a vertical temperature map for each bin and have them all shown on the same plot?
The violin plot mentioned in the comments was a nice solution to my problem. Here's where I found a python implementation of it - it would certainly be nice if this were included into matplotlib eventually. Overplotted is a box plot centered on the median value, and includes the 2nd and 3rd quartiles.