Plotting triangular of matrix with diagonal at bottom - r

I would like to plot data which is defined only for each unordered pair of distinct elements in a set. This is naturally represented by a lower-- or upper--triangular matrix, with no values on the diagonal. A correlation matrices is an example.
I might plot it like this
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
mat = np.random.randn(12,12)
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(mat, dtype=bool))
sns.heatmap(mat, mask=mask, cmap="vlag", square=True, linewidths=.5, cbar=None)
giving
But I would prefer to plot it like this
Or perhaps with the labels showing each once on the hypoteneuse/diagonal would be nicer, rather than repeated on the two sides like I have done.
Is there a natural way to do this with pyplot or seaborn (or R)? I'm sure there's a relatively simple hacky way, but I wonder if there's a package out there that already does something like this. It seems like a natural way to represent symmetric relation data.

Related

How to create a simples (ternary) plot with color-coded triangles in R?

I have a matrix with 4 variables whereas 3 variables are parameters and the 4th variable gives the mean sum of squares for simulation results with the corresponding variables. Now I'd like to create a ternary plot with R where the triangle corresponding to the 3 parameter values should be colored by the mean sum of squares value. Alternatively, I'd like to plot interpolated mean sum of squares in the whole simplex triangle.
I was already looking for some functions or code that does what I'm looking for. But I didn't succeed.
Nevertheless, here's an example code of how my data set looks like (for which I'd like to create the ternary plot):
grid <- as.matrix(expand.grid(seq(0,0.5,0.025), seq(0,0.5,0.025), seq(-0.25,0.25,0.025)))
data <- cbind (grid, runif(9261,0,2))
I'd be very thankful if you'd provide R code that can create the plot I'd like to get. Maybe there's even a pre-implemented function in a package that I haven't found?!
Thanks a lot in advance for your help!

Online tool for picking points on the 2D plane and exporting them to coordinates)

I am looking for a specific online tool. At first it displays empty 2D plot (with gridlines from -10 to 10 for example). You can also choose a color. When I select a color and then click on the plot a new point should be drawn on the plot. I can click multiple times so that multiple points are generated on the plot. Then I can change the color and generate more points on the same plot (but with different color). When I'm done I should be able to export the points to list of coordinates and color: [(0, 1, 'blue'), (1, 1, 'green'), (1, 2, 'green')].
Does anyone know such tool? It's purpose is to simply quickly generate 2D dataset with multiple classes.
I wasn't able to find a tool that would exactly meet all your requirements but I think there is a solution that my fulfill some of them.
You can use plotly (https://plot.ly/create/) to plot visualize the points using scatter plot creator.
As for random points you can generate them randomly as well as assign colors to them using some simple python function, like this:
import pandas as pd
import numpy as np
import random
def make_points(minv,maxv,total):
df = pd.DataFrame(np.random.uniform(low=minv, high=maxv, size=(total,2)), columns=list('XY'))
arr=["blue", "green", "purple", "red"]
arr *= total // len(arr)
random.shuffle(arr)
df['color'] = arr
df.to_csv("points")
return df
make_points(-10,10,100)
This for example will create a dataframe with 100 2d points that can get values from -10, 10, and each is randomly assigned one of 4 colors.
Import the csv in the plotly chart creator and you can then manually edit the values if you like.

Plots.jl - Map surface color to matrix

I'm trying to figure out how to create surface plots with Plots.jl. I can create a spherical surface from a 2-d mesh like below:
using Plots
plotlyjs()
# Read the theta and phi angles from file...
x2d = sind(theta2d).*cosd(phi2d)
y2d = sind(theta2d).*sind(phi2d)
z2d = cosd(theta2d)
surface(x2d,y2d,z2d)
However, I want to have the surface color be controlled by a separate matrix like the plot below (made with Python).
from mayavi import mlab
# Create/read plot data...
mlab.figure(bgcolor=(1,1,1), fgcolor=(0.,0.,0.))
mlab.mesh(x2d, y2d, z2d, scalars=p2d, colormap='jet', vmax=5, vmin=-35)
Perhaps I should just use the Python plotting functions directly? Or maybe GLVisualize directly?
Thanks!
You can supply the matrix controlling the colors as the fill_z keyword. It doesn't work on all backends, but try plotljys and pyplot.

newbie: holoviews Curves from pandas dataset columns

I have a pandas dataframe with columns labeled...
x
y
true_x
true_y
I would like to plot a curve of true_x vs true_y overlaid with
points y vs x.
The tutorials leave me baffled since they only describe simple 2D and 3D examples.
We're about to start working extensively on additional documentation so that's good feedback. To create a simple plot like that simply declare a Curve and a Scatter object each with the appropriate kdims and vdims and overlay them using the mul operator:
curve = hv.Curve(df, kdims=['true_x'], vdims=['true_y'])
scatter = hv.Scatter(df, kdims=['x'], vdims=['y'])
curve * scatter

Why is my plot3d white in SciLab?

t = 0:%pi/50:10*%pi;
plot3d(sin(t),cos(t),t)
When I execute this code the plot is done but the line is not visible, only the box. Any ideas which property I have to change?
Thanks
The third argument should, in this case, be a matrix of the size (length arg1) x (length arg2).
You'd expect plot3d to behave like an extension of plot and plot2d but it isn't quite the case.
The 2d plot takes a vector of x and a vector of y and plots points at (x1,y1), (x2,y2) etc., joined with lines or not as per style settings. That fits the conceptual model we usually use for 2d plots - charting the relationship of one thing as a function of another, in most cases (y = f(x)). THere are other ways to use a 2d plot: scatter graphs are common but it's easy enough to produce one using the two-rows-of-data concept.
This doesn't extend smoothly to 3d though as there are many other ways you could use a 3d plot to represent data. If you gave it three vectors of coordinates and asked it to draw a line between them all what might we want to use that for? Is that the most useful way of using a 3d plot?
Most packages give you different visualisation types for the different kinds of data. Mathematica has a lot of 3d visualisation types and Python/Scipy/Mayavi2 has even more. Matlab has a number too but Scilab, while normally mirroring Matlab, in this case prefers to handle it all with the plot3d function.
I think of it like a contour plot: you give it a vector of x and a vector of y and it uses those to create a grid of (x,y) points. The third argument is then a matrix whose dimensions match those of the (x,y) grid holding the z-coordinates of each point. The first example in the docs does what I think you're after:
t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);
The first line creates a column vector of length 21
-->size(t)
ans =
21. 1.
The second line computes a 21 x 21 matrix of products of the permutations of sin(t) with cos(t) - note the transpose in the cos(t') element.
-->size(z)
ans =
21. 21.
Then when it plots them it draws (x1,y1,z11), (x1,y2,x12), (x2,y2,z22) and so on. It draws lines between adjacent points in a mesh, or no lines, or just the surface.

Resources