Pie plot using Plotly in Python

Pie plot using Plotly in Python

To create a pie plot using Plotly in Python, you need to use the plotly.graph_objects module. Here's a step-by-step guide on how to do it:

1. Install Plotly:

If you haven't installed Plotly yet, you can do so with pip:

pip install plotly 

2. Create a Pie Plot using Plotly:

Here's a simple example to create a pie plot:

import plotly.graph_objects as go # Data labels = ['Apples', 'Bananas', 'Cherries', 'Dates'] values = [4500, 2500, 1050, 750] # Create Pie Plot fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)]) # Show plot fig.show() 

In the example above:

  • We use the go.Pie class to create a pie plot.
  • labels and values define the categories and their respective sizes in the pie chart.
  • The hole parameter is optional and creates a "donut" chart if set to a value between 0 and 1.

Customizations:

You can customize the appearance and behavior of the pie chart using various parameters:

fig = go.Figure(data=[go.Pie( labels=labels, values=values, pull=[0, 0.2, 0, 0], # 'pull' is given as a fraction of the pie radius. It makes the 'Bananas' slice stand out. textinfo='label+percent', # Show label and percentage insidetextorientation='radial' # Text orientation )]) fig.show() 

Plotly is very flexible and allows for a variety of customizations, interactivity, and other features that make your plots more informative and visually appealing.


More Tags

android-proguard junit5 dictionary blazor-server-side inner-join makefile client-certificates electron-builder preload laravel-5.1

More Programming Guides

Other Guides

More Programming Examples