Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Last active December 21, 2015 11:28
Show Gist options
  • Save chriddyp/6298748 to your computer and use it in GitHub Desktop.
Save chriddyp/6298748 to your computer and use it in GitHub Desktop.
MATLAB code to generate the "Math Score Improvement" (https://plot.ly/~chris/1010/) graph using the Plotly MATLAB API. To run, follow the Plotly-MATLAB API installation instructions on Plotly's API page (https://plot.ly/api) where you can also find the formal Plotly graphing syntax and more MATLAB examples. Questions? Email chris [at] plot [dot] ly
% Math score improvement - Box plot with line chart
% Script generates a graph in your browser that looks like this: https://plot.ly/~chris/1010/
username = 'xxxxxx' % your plotly username
api_key = 'xxxxxx' % your plotly api key
signin(username, api_key) % sign-in to plotly
% mean scores
means = [80, 85, 81, 89, 94, 96];
N = 6; % Number of trials
ns = 10; % Samples per trial
% trial labels
trial_labels = cell{N,1}
for i=1:N
trial_labels{i} = ['Trial ' num2str(i)];
end
% box plots of normally distributed random data with random standard deviation
% also place the underlying data points next to the box-plot with the 'boxpoints' key
data = cell(N+1,1);
for i=1:N
data{i} = struct('y', 1+2*rand(1)+randn(ns,1)+means(i),'name',trial_labels{i});
end
% style the box plots
style = struct('type', 'box', ...
'boxpoints', 'all', ...
'jitter', 0.3, ...
'pointpos', -1.8, ...
'fillcolor', 'rgba(255, 255, 255, 0)', ...
'line',struct('color','rgb(0, 0, 255)'), ...
'marker',struct('color','rgba(255, 255, 255, 0)', ...
'line',struct('color','black', 'width',1)));
% line plot of the median scores
medians = zeros(N,1);
for i=1:N
medians(i) = median(trial_data{i}.y);
end
trial_median = struct('x', trial_labels, ...
'y', medians, ...
'type','scatter', ...
'mode','lines', ...
'line',struct('color','rgba(255, 0, 0, 0.5)'));
data{N+1}=trial_median;
layout = struct( 'title', 'Math Score Improvement', ...
'xaxis',struct('showgrid',false,'zeroline',false, ...
'linecolor','rgba(255, 255, 255, 0)'), ...
'yaxis',struct('linecolor','rgba(255, 255, 255, 0)', ...
'title', 'Score'), ...
'font', struct('family', 'Avant Garde', ...
'size', 14, ...
'color', 'rgb(67, 67, 67)'), ...
'showlegend',false);
kwargs = struct('layout',layout,'style',style,'filename','matlab mathscores','fileopt','overwrite')
response = plotly(trial_data, kwargs)
url = response.url % View the graph in your browser at this url
filename = response.filename % or in your plotly account with this filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment