Plotly – Box Plot Violin Plot and Contour Plot ”; Previous Next This chapter focusses on detail understanding about various plots including box plot, violin plot, contour plot and quiver plot. Initially, we will begin with the Box Plot follow. Box Plot A box plot displays a summary of a set of data containing the minimum, first quartile, median, third quartile, and maximum. In a box plot, we draw a box from the first quartile to the third quartile. A vertical line goes through the box at the median. The lines extending vertically from the boxes indicating variability outside the upper and lower quartiles are called whiskers. Hence, box plot is also known as box and whisker plot. The whiskers go from each quartile to the minimum or maximum. To draw Box chart, we have to use go.Box() function. The data series can be assigned to x or y parameter. Accordingly, the box plot will be drawn horizontally or vertically. In following example, sales figures of a certain company in its various branches is converted in horizontal box plot. It shows the median of minimum and maximum value. trace1 = go.Box(y = [1140,1460,489,594,502,508,370,200]) data = [trace1] fig = go.Figure(data) iplot(fig) The output of the same will be as follows − The go.Box() function can be given various other parameters to control the appearance and behaviour of box plot. One such is boxmean parameter. The boxmean parameter is set to true by default. As a result, the mean of the boxes” underlying distribution is drawn as a dashed line inside the boxes. If it is set to sd, the standard deviation of the distribution is also drawn. The boxpoints parameter is by default equal to “outliers“. Only the sample points lying outside the whiskers are shown. If “suspectedoutliers”, the outlier points are shown and points either less than 4″Q1-3″Q3 or greater than 4″Q3-3″Q1 are highlighted. If “False”, only the box(es) are shown with no sample points. In the following example, the box trace is drawn with standard deviation and outlier points. trc = go.Box( y = [ 0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25 ], boxpoints = ”suspectedoutliers”, boxmean = ”sd” ) data = [trc] fig = go.Figure(data) iplot(fig) The output of the same is stated below − Violin Plot Violin plots are similar to box plots, except that they also show the probability density of the data at different values. Violin plots will include a marker for the median of the data and a box indicating the interquartile range, as in standard box plots. Overlaid on this box plot is a kernel density estimation. Like box plots, violin plots are used to represent comparison of a variable distribution (or sample distribution) across different “categories”. A violin plot is more informative than a plain box plot. In fact, while a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data. Violin trace object is returned by go.Violin() function in graph_objects module. In order to display underlying box plot, the boxplot_visible attribute is set to True. Similarly, by setting meanline_visible property to true, a line corresponding to the sample”s mean is shown inside the violins. Following example demonstrates how Violin plot is displayed using plotly’s functionality. import numpy as np np.random.seed(10) c1 = np.random.normal(100, 10, 200) c2 = np.random.normal(80, 30, 200) trace1 = go.Violin(y = c1, meanline_visible = True) trace2 = go.Violin(y = c2, box_visible = True) data = [trace1, trace2] fig = go.Figure(data = data) iplot(fig) The output is as follows − Contour plot A 2D contour plot shows the contour lines of a 2D numerical array z, i.e. interpolated lines of isovalues of z. A contour line of a function of two variables is a curve along which the function has a constant value, so that the curve joins points of equal value. A contour plot is appropriate if you want to see how some value Z changes as a function of two inputs, X and Y such that Z = f(X,Y). A contour line or isoline of a function of two variables is a curve along which the function has a constant value. The independent variables x and y are usually restricted to a regular grid called meshgrid. The numpy.meshgrid creates a rectangular grid out of an array of x values and an array of y values. Let us first create data values for x, y and z using linspace() function from Numpy library. We create a meshgrid from x and y values and obtain z array consisting of square root of x2+y2 We have go.Contour() function in graph_objects module which takes x,y and z attributes. Following code snippet displays contour plot of x, y and z values computed as above. import numpy as np xlist = np.linspace(-3.0, 3.0, 100) ylist = np.linspace(-3.0, 3.0, 100) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) trace = go.Contour(x = xlist, y = ylist, z = Z) data = [trace] fig = go.Figure(data) iplot(fig) The output is as follows − The contour plot can be customized by one or more of following parameters − Transpose (boolean) − Transposes the z data. If xtype (or ytype) equals “array”, x/y coordinates are given by “x”/”y”. If “scaled”, x coordinates are given by “x0” and “dx“. The connectgaps parameter determines whether or not gaps in the z data are filled in. Default value of ncontours parameter is 15. The actual number of contours will be chosen automatically to be less than or equal to the value of `ncontours`. Has an effect only
Category: plotly
Plotly – Legends
Plotly – Legends ”; Previous Next By default, Plotly chart with multiple traces shows legends automatically. If it has only one trace, it is not displayed automatically. To display, set showlegend parameter of Layout object to True. layout = go.Layoyt(showlegend = True) Default labels of legends are trace object names. To set legend label explicitly set name property of trace. In following example, two scatter traces with name property are plotted. import numpy as np import math #needed for definition of pi xpoints = np.arange(0, math.pi*2, 0.05) y1 = np.sin(xpoints) y2 = np.cos(xpoints) trace0 = go.Scatter( x = xpoints, y = y1, name=”Sine” ) trace1 = go.Scatter( x = xpoints, y = y2, name = ”cos” ) data = [trace0, trace1] layout = go.Layout(title = “Sine and cos”, xaxis = {”title”:”angle”}, yaxis = {”title”:”value”}) fig = go.Figure(data = data, layout = layout) iplot(fig) The plot appears as below − Print Page Previous Next Advertisements ”;
Scatter Plot, Scattergl Plot and Bubble Charts ”; Previous Next This chapter emphasizes on details about Scatter Plot, Scattergl Plot and Bubble Charts. First, let us study about Scatter Plot. Scatter Plot Scatter plots are used to plot data points on a horizontal and a vertical axis to show how one variable affects another. Each row in the data table is represented by a marker whose position depends on its values in the columns set on the X and Y axes. The scatter() method of graph_objs module (go.Scatter) produces a scatter trace. Here, the mode property decides the appearance of data points. Default value of mode is lines which displays a continuous line connecting data points. If set to markers, only the data points represented by small filled circles are displayed. When mode is assigned ‘lines+markers’, both circles and lines are displayed. In the following example, plots scatter traces of three sets of randomly generated points in Cartesian coordinate system. Each trace displayed with different mode property is explained below. import numpy as np N = 100 x_vals = np.linspace(0, 1, N) y1 = np.random.randn(N) + 5 y2 = np.random.randn(N) y3 = np.random.randn(N) – 5 trace0 = go.Scatter( x = x_vals, y = y1, mode = ”markers”, name = ”markers” ) trace1 = go.Scatter( x = x_vals, y = y2, mode = ”lines+markers”, name = ”line+markers” ) trace2 = go.Scatter( x = x_vals, y = y3, mode = ”lines”, name = ”line” ) data = [trace0, trace1, trace2] fig = go.Figure(data = data) iplot(fig) The output of Jupyter notebook cell is as given below − Scattergl Plot WebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics within any compatible web browser without the use of plug-ins. WebGL is fully integrated with other web standards, allowing Graphics Processing Unit (GPU) accelerated usage of image processing. Plotly you can implement WebGL with Scattergl() in place of Scatter() for increased speed, improved interactivity, and the ability to plot even more data. The go.scattergl() function which gives better performance when a large number of data points are involved. import numpy as np N = 100000 x = np.random.randn(N) y = np.random.randn(N) trace0 = go.Scattergl( x = x, y = y, mode = ”markers” ) data = [trace0] layout = go.Layout(title = “scattergl plot “) fig = go.Figure(data = data, layout = layout) iplot(fig) The output is mentioned below − Bubble charts A bubble chart displays three dimensions of data. Each entity with its three dimensions of associated data is plotted as a disk (bubble) that expresses two of the dimensions through the disk”s xy location and the third through its size. The sizes of the bubbles are determined by the values in the third data series. Bubble chart is a variation of the scatter plot, in which the data points are replaced with bubbles. If your data has three dimensions as shown below, creating a Bubble chart will be a good choice. Company Products Sale Share A 13 2354 23 B 6 5423 47 C 23 2451 30 Bubble chart is produced with go.Scatter() trace. Two of the above data series are given as x and y properties. Third dimension is shown by marker with its size representing third data series. In the above mentioned case, we use products and sale as x and y properties and market share as marker size. Enter the following code in Jupyter notebook. company = [”A”,”B”,”C”] products = [13,6,23] sale = [2354,5423,4251] share = [23,47,30] fig = go.Figure(data = [go.Scatter( x = products, y = sale, text = [ ”company:”+c+” share:”+str(s)+”%” for c in company for s in share if company.index(c)==share.index(s) ], mode = ”markers”, marker_size = share, marker_color = [”blue”,”red”,”yellow”]) ]) iplot(fig) The output would be as shown below − Print Page Previous Next Advertisements ”;
Plotly – Exporting to Static Images ”; Previous Next Outputs of offline graphs can be exported to various raster and vector image formats. For that purpose, we need to install two dependencies – orca and psutil. Orca Orca stands for Open-source Report Creator App. It is an Electron app that generates images and reports of plotly graphs, dash apps, dashboards from the command line. Orca is the backbone of Plotly”s Image Server. psutil psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization in Python. It implements many functionalities offered by UNIX command line tools such as: ps, top, netstat, ifconfig, who, etc. psutil supports all major operating systems such as Linux, Windows and MacOs Installation of Orca and psutil If you are using Anaconda distribution of Python, installation of orca and psutil is very easily done by conda package manager as follows − conda install -c plotly plotly-orca psutil Since, orca is not available in PyPi repository. You can instead use npm utility to install it. npm install -g electron@1.8.4 orca Use pip to install psutil pip install psutil If you are not able to use npm or conda, prebuilt binaries of orca can also be downloaded from the following website which is available at https://github.com/plotly/orca/releases. To export Figure object to png, jpg or WebP format, first, import plotly.io module import plotly.io as pio Now, we can call write_image() function as follows − pio.write_image(fig, ‘sinewave.png’) pio.write_image(fig, ‘sinewave.jpeg’) pio.write_image(fig,’sinewave.webp) The orca tool also supports exporting plotly to svg, pdf and eps formats. Pio.write_image(fig, ‘sinewave.svg’) pio.write_image(fig, ‘sinewave.pdf’) In Jupyter notebook, the image object obtained by pio.to_image() function can be displayed inline as follows − Print Page Previous Next Advertisements ”;
Plotly – Plotting Inline with Jupyter Notebook ”; Previous Next In this chapter, we will study how to do inline plotting with the Jupyter Notebook. In order to display the plot inside the notebook, you need to initiate plotly’s notebook mode as follows − from plotly.offline import init_notebook_mode init_notebook_mode(connected = True) Keep rest of the script as it is and run the notebook cell by pressing Shift+Enter. Graph will be displayed offline inside the notebook itself. import plotly plotly.tools.set_credentials_file(username = ”lathkar”, api_key = ”************”) from plotly.offline import iplot, init_notebook_mode init_notebook_mode(connected = True) import plotly import plotly.graph_objs as go import numpy as np import math #needed for definition of pi xpoints = np.arange(0, math.pi*2, 0.05) ypoints = np.sin(xpoints) trace0 = go.Scatter( x = xpoints, y = ypoints ) data = [trace0] plotly.offline.iplot({ “data”: data,”layout”: go.Layout(title=”Sine wave”)}) Jupyter notebook output will be as shown below − The plot output shows a tool bar at top right. It contains buttons for download as png, zoom in and out, box and lasso, select and hover. Print Page Previous Next Advertisements ”;
Plotly – Online and Offline Plotting ”; Previous Next The following chapter deals with the settings for the online and offline plotting. Let us first study the settings for online plotting. Settings for online plotting Data and graph of online plot are save in your plot.ly account. Online plots are generated by two methods both of which create a unique url for the plot and save it in your Plotly account. py.plot() − returns the unique url and optionally open the url. py.iplot() − when working in a Jupyter Notebook to display the plot in the notebook. We shall now display simple plot of angle in radians vs. its sine value. First, obtain ndarray object of angles between 0 and 2π using arange() function from numpy library. This ndarray object serves as values on x axis of the graph. Corresponding sine values of angles in x which has to be displayed on y axis are obtained by following statements − import numpy as np import math #needed for definition of pi xpoints = np.arange(0, math.pi*2, 0.05) ypoints = np.sin(xpoints) Next, create a scatter trace using Scatter() function in graph_objs module. trace0 = go.Scatter( x = xpoints, y = ypoints ) data = [trace0] Use above list object as argument to plot() function. py.plot(data, filename = ”Sine wave”, auto_open=True) Save following script as plotly1.py import plotly plotly.tools.set_credentials_file(username=”lathkar”, api_key=”********************”) import plotly.plotly as py import plotly.graph_objs as go import numpy as np import math #needed for definition of pi xpoints = np.arange(0, math.pi*2, 0.05) ypoints = np.sin(xpoints) trace0 = go.Scatter( x = xpoints, y = ypoints ) data = [trace0] py.plot(data, filename = ”Sine wave”, auto_open=True) Execute the above mentioned script from command line. Resultant plot will be displayed in the browser at specified URL as stated below. $ python plotly1.py High five! You successfully sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~lathkar/0 Just above the displayed graph, you will find tabs Plot, Data, Python & Rand Forking history. Currently, Plot tab is selected. The Data tab shows a grid containing x and y data points. From Python & R tab, you can view code corresponding to current plot in Python, R, JSON, Matlab etc. Following snapshot shows Python code for the plot as generated above − Setting for Offline Plotting Plotly allows you to generate graphs offline and save them in local machine. The plotly.offline.plot() function creates a standalone HTML that is saved locally and opened inside your web browser. Use plotly.offline.iplot() when working offline in a Jupyter Notebook to display the plot in the notebook. Note − Plotly”s version 1.9.4+ is needed for offline plotting. Change plot() function statement in the script and run. A HTML file named temp-plot.html will be created locally and opened in web browser. plotly.offline.plot( { “data”: data,”layout”: go.Layout(title = “hello world”)}, auto_open = True) Print Page Previous Next Advertisements ”;
Plotly – Home
Plotly Tutorial PDF Version Quick Guide Resources Job Search Discussion This tutorial is about Canada based technical computing company Plotly which is also known for its URL. Here, you will learn about how to develop data analytics and visualization tools. Moreover, this tutorial describes the features of Plotly’s Python graphing library to make interactive and publication-ready graphs for both online and offline viewing. Audience The tutorial is aptly designed for all those who are passionate about learning online graphing, analytics, and statistics tools. Furthermore, it is for those individuals who have keen interest in understanding how Plotly helps in providing tools for scientific graphing libraries of the computer programming languages such as Python, R, MATLAB, Perl, Julia, Arduino, and REST. Prerequisites To work with Plotly, you need to create an account on the official website. The details about how to create an account and get login is discussed in the tutorial. If you are novice to knowledge about data analytics, visualization tools or any of the programming languages like Python, R, MATLAB, Arduino, REST, Julia and Perl, we suggest you to go through tutorials related to these before proceeding with this tutorial. Print Page Previous Next Advertisements ”;
Plotly – Subplots and Inset Plots ”; Previous Next Here, we will understand the concept of subplots and inset plots in Plotly. Making Subplots Sometimes it is helpful to compare different views of data side by side. This supports the concept of subplots. It offers make_subplots() function in plotly.tools module. The function returns a Figure object. The following statement creates two subplots in one row. fig = tools.make_subplots(rows = 1, cols = 2) We can now add two different traces (the exp and log traces in example above) to the figure. fig.append_trace(trace1, 1, 1) fig.append_trace(trace2, 1, 2) The Layout of figure is further configured by specifying title, width, height, etc. using update() method. fig[”layout”].update(height = 600, width = 800s, title = ”subplots”) Here”s the complete script − from plotly import tools import plotly.plotly as py import plotly.graph_objs as go from plotly.offline import iplot, init_notebook_mode init_notebook_mode(connected = True) import numpy as np x = np.arange(1,11) y1 = np.exp(x) y2 = np.log(x) trace1 = go.Scatter( x = x, y = y1, name = ”exp” ) trace2 = go.Scatter( x = x, y = y2, name = ”log” ) fig = tools.make_subplots(rows = 1, cols = 2) fig.append_trace(trace1, 1, 1) fig.append_trace(trace2, 1, 2) fig[”layout”].update(height = 600, width = 800, title = ”subplot”) iplot(fig) This is the format of your plot grid: [ (1,1) x1,y1 ] [ (1,2) x2,y2 ] Inset Plots To display a subplot as inset, we need to configure its trace object. First the xaxis and yaxis properties of inset trace to ‘x2’ and ‘y2’ respectively. Following statement puts ‘log’ trace in inset. trace2 = go.Scatter( x = x, y = y2, xaxis = ”x2”, yaxis = ”y2”, name = ”log” ) Secondly, configure Layout object where the location of x and y axes of inset is defined by domain property that specifies is position with respective to major axis. xaxis2=dict( domain = [0.1, 0.5], anchor = ”y2” ), yaxis2 = dict( domain = [0.5, 0.9], anchor = ”x2” ) Complete script to display log trace in inset and exp trace on main axis is given below − trace1 = go.Scatter( x = x, y = y1, name = ”exp” ) trace2 = go.Scatter( x = x, y = y2, xaxis = ”x2”, yaxis = ”y2”, name = ”log” ) data = [trace1, trace2] layout = go.Layout( yaxis = dict(showline = True), xaxis2 = dict( domain = [0.1, 0.5], anchor = ”y2” ), yaxis2 = dict( showline = True, domain = [0.5, 0.9], anchor = ”x2” ) ) fig = go.Figure(data=data, layout=layout) iplot(fig) The output is mentioned below − Print Page Previous Next Advertisements ”;
Plotly – Package Structure
Plotly – Package Structure ”; Previous Next Plotly Python package has three main modules which are given below − plotly.plotly plotly.graph_objs plotly.tools The plotly.plotly module contains functions that require a response from Plotly”s servers. Functions in this module are interface between your local machine and Plotly. The plotly.graph_objs module is the most important module that contains all of the class definitions for the objects that make up the plots you see. Following graph objects are defined − Figure, Data, ayout, Different graph traces like Scatter, Box, Histogram etc. All graph objects are dictionary- and list-like objects used to generate and/or modify every feature of a Plotly plot. The plotly.tools module contains many helpful functions facilitating and enhancing the Plotly experience. Functions for subplot generation, embedding Plotly plots in IPython notebooks, saving and retrieving your credentials are defined in this module. A plot is represented by Figure object which represents Figure class defined in plotly.graph_objs module. It’s constructor needs following parameters − import plotly.graph_objs as go fig = go.Figure(data, layout, frames) The data parameter is a list object in Python. It is a list of all the traces that you wish to plot. A trace is just the name we give to a collection of data which is to be plotted. A trace object is named according to how you want the data displayed on the plotting surface. Plotly provides number of trace objects such as scatter, bar, pie, heatmap etc. and each is returned by respective functions in graph_objs functions. For example: go.scatter() returns a scatter trace. import numpy as np import math #needed for definition of pi xpoints=np.arange(0, math.pi*2, 0.05) ypoints=np.sin(xpoints) trace0 = go.Scatter( x = xpoints, y = ypoints ) data = [trace0] The layout parameter defines the appearance of the plot, and plot features which are unrelated to the data. So we will be able to change things like the title, axis titles, annotations, legends, spacing, font and even draw shapes on top of your plot. layout = go.Layout(title = “Sine wave”, xaxis = {”title”:”angle”}, yaxis = {”title”:”sine”}) A plot can have plot title as well as axis title. It also may have annotations to indicate other descriptions. Finally, there is a Figure object created by go.Figure() function. It is a dictionary-like object that contains both the data object and the layout object. The figure object is eventually plotted. py.iplot(fig) Print Page Previous Next Advertisements ”;