Show Sidebar Hide Sidebar

Getting Started with Plotly for MATLAB

Get started with Plotly's MATLAB graphing library to make interactive, publication-quality graphs online.

Installation

Download and uncompress the Plotly MATLAB library.

Initialization

In your MATLAB console, begin by locating the downloaded directory, and running plotlysetup().

>> cd ~/Downloads/MATLAB-api-master
>> plotlysetup('DemoAccount', 'lr1c37zw81')

You'll need to replace "DemoAccount" and "lr1c37zw81" with your Plotly username and API key.

Find my API key.

You only have to set this up if it's your first time using a Plotly API.

This will set up your credentials and import the Plotly library into your MATLAB toolboxes. You can now delete the downloaded folder.

Update

Please note that Plotly's update function: plotlyupdate is temporarily disabled.

To update, download the latest version here.

Once downloaded, run plotlysetup('your_username', 'your_api_key') to get started with the latest version.

Special Instructions for Plotly On-Premise Users

Your API key for a Plotly On-Premise account will be different from an account on the Plotly cloud. Visit https://plotly.your-company.com/settings/api/ to find your Plotly On-Premise API key. Remember to replace "your-company.com" with the URL of your Plotly On-Premise server.

Additionally, you'll have to change the MATLAB API endpoint so that it points to your company's Plotly server instead of Plotly's cloud. In your MATLAB console, enter:


>> saveplotlyconfig('https://plotly.your-company.com', 'stream-plotly.your-company.com')

Alternatively, if you'd like to set your configuration to create private plots by default you should enter:


>> saveplotlyconfig('https://plotly.your-company.com', 'stream-plotly.your-company.com', 'private')

Make sure to replace "your-company.com" with the URL of your Plotly On-Premise server.Questions? support@plot.ly

Start Plotting!

Now you can convert your MATLAB figures into beautiful, interactive, online graphs with fig2plotly():

[X,Y,Z] = peaks;
contour(X,Y,Z,20);

fig2plotly()

Note: fig2plotly() will render your plot using Plotly's own color and styling by default. To preserve your original MATLAB color and styling, use: fig2plotly(gcf,'strip',false).

Start Plotting Offline!

Now you can convert your MATLAB figures into beautiful, interactive, Plotly graphs offline with fig2plotly(). The graphs will be saved as html files to your current working directory and contain all of the necessary dependencies to render the plot within a browser offline.
First download the offline Plotly bundle:

getplotlyoffline('https://cdn.plot.ly/plotly-latest.min.js')

Then add the 'offline' flag to fig2plotly().

[X,Y,Z] = peaks;
contour(X,Y,Z,20);

fig2plotly(gcf, 'offline', true)

Learn more about using Plotly Offline for Matlab.

Response

Copy and paste the URL into your browser to see your graph!

url: 'https://plot.ly/~MATLABAPI/72'
message: [1x0 char]
warning: [1x0 char]
filename: 'untitled'
error: [1x0 char]

Credentials

The initialization step places a special .plotly/.credentials file in your home directory. Your ~/.plotly/.credentials file should look something like this:


{
    "username": "DemoAccount",
    "stream_ids": ["ylosqsyet5", "h2ct8btk1s", "oxz4fm883b"],
    "api_key": "lr1c37zw81"
}
            

You can change the contents of this file manually or as described in the Initialization section.

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.