Plotly.js is a charting library that comes with over 40 chart types, 3D charts, statistical graphs, and SVG maps. Show
Scatter PlotsSource Code var xArray = [50,60,70,80,90,100,110,120,130,140,150]; // Define Data // Define Layout Plotly.newPlot("myPlot", data, layout); Try it Yourself » Line GraphsSource Code var xArray =
[50,60,70,80,90,100,110,120,130,140,150]; // Define Data // Define Layout //
Display using Plotly Try it Yourself » Linear GraphsSource Codevar exp = "x + 17"; // Generate values // Define
Data // Define Layout // Display using Plotly Try it Yourself » Multiple LinesSource Code var exp1 = "x"; // Generate values var x1Values = []; for (var x = 0; x <= 10; x += 1) { //
Define Data // Define Layout // Display using Plotly Try it Yourself » Bar ChartsSource Code var xArray = ["Italy","France","Spain","USA","Argentina"]; var data = [{ Plotly.newPlot("myPlot", data, layout); Try it Yourself » Horizontal Bar ChartsSource Code var xArray = [55, 49, 44, 24, 15]; var data = [{ var layout = {title:"World Wine Wine Production"}; Plotly.newPlot("myPlot", data, layout); Try it Yourself » Pie ChartsTo display a pie instead of bars, change x and y to labels and values, and change the type to "pie": Donut ChartsTo display a donut instead of a pie, add a hole: var data = [{ Try it Yourself » Plotting EquationsTry it Yourself » Source Codevar exp = "Math.sin(x)"; // Generate values // Display using Plotly Try it Yourself » IntroductionPlotly is a JavaScript-based, Python data visualization library, focused on interactive and web-based visualizations. It has the simplicity of Seaborn, with a high-level API, but also the interactivity of Bokeh. In addition to the core library's functionality, using the built-in Plotly Express with Dash, makes it an amazing choice for web-based applications and interactive, data-driven dashboards, usually written in Flask. In this guide, we'll take a look at how to plot a Bar Plot with Plotly.
Plot a Bar Plot with PlotlyTo plot a Bar Plot in Plotly, you simply call the
Here, we have three categories, as a list that we've provided to the However, we rarely work with lists when doing Data Visualization. Let's import the Telecom Users Dataset and
visualize the The
This results in:
Now, let's go ahead and plot this data as a Bar Plot:
To plot data from a dataset, we've provided the data source ( Now, the plot generated by Plotly actually separates each instance into a small stacked bar of its own on this plot, since several rows share the same We can see the cumulative number of months they've served to their customers, in parallel.
While ~90K months may seem like an insane number of months (7500 years), the median
Change Bar Plot Colors with PlotlyIt's easy to change the colors of each of the aforementioned instances, based on any other variable present in the dataset. These are most often other categorical features, such as Grouping instances by another feature creates a Grouped Bar Plot, which are most commonly plotted one next to the other. By simply coloring instances based on another feature, we'll have a Stacked Grouped Bar Plot, since we'll have two or more groups of instances one on top of the other. Let's take a look at the Bar Plot once again, once we color each plot depending on the
Now, the default Plotly color-scheme will apply, and the instances will be sorted by color so they're not mish-mashed throughout the plot: Since we've got multiple rows sharing the same If we were to plot a different feature, that doesn't stack (isn't a binary choice of another feature), the plot would look fairly differently:
When providing many color values, such as Plot Grouped Bar Plot with PlotlySometimes, it's easier to differentiate if we plot the bars one next to the other, instead of stacking them on top of each other. This is particularly useful when we have binary features, such as To do this, we can simply update the layout of the Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Now, it's much easier to compare the number of instances: Plot a Horizontal Bar Plot with PlotlyTo plot a Bar Plot horizontally using Plotly, we can set the
Keep in mind that if you'd like to keep the X-Y relationship as it was before, you'll switch the Ordering Bar Plots in PlotlyPlotly automatically orders the bars. You can directly modify this setting by updating the You can opt for manual ordering, or automatic ordering. For manual ordering, you can pass in a
dictionary that contains the names of the features and their order, which will result in a fixed order, to the
Here, we've ordered the Plotting the Bar Plot now will result in: Alternatively, especially if there's a large number of possible values - we might want to let Plotly automatically sort and order values of features. We can either sort them categorically or numerically. And both of these can further be ordered in By default, Plotly adjusts the ordering based on the provided data - in our case, it ordered it as
This results in: When ordering categorically, it sorts lexicographically:
This results in: ConclusionIn this tutorial, we've taken a quick look at how to plot and customize Bar Plots with Plotly. If you're interested in Data Visualization and don't know where to start, make sure to check out our bundle of books on Data Visualization in Python: Data Visualization in Python with Matplotlib and Pandas is a book designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and allow them to build a strong foundation for advanced work with theses libraries - from simple plots to animated 3D plots with interactive buttons.
Data Visualization in Python, a book for beginner to intermediate Python developers, guides you through simple data manipulation with Pandas, cover core plotting libraries like Matplotlib and Seaborn, and show you how to take advantage of declarative and experimental libraries like Altair. More specifically, over the span of 11 chapters this book covers 9 Python libraries: Pandas, Matplotlib, Seaborn, Bokeh, Altair, Plotly, GGPlot, GeoPandas, and VisPy.
|