How to subplot pie chart in plotly?

2024/11/17 23:55:58

How can I subplot 'pie1' in 'fig', so it be located at 'the first' position. this is how I am doing it but it doesn't work out

    import pandas as pdimport numpy as npimport seaborn as snsimport plotly.offline as pypimport plotly.graph_objs as gofrom plotly import toolsimport plotly.plotly as pyfrom plotly.offline import iplot,init_notebook_modefrom IPython.core.display import HTMLimport plotly.iodf1=pd.read_excel('file.xlsx',sheet_name='sheet1',index=False)con_pivot=pd.pivot_table(con,index='Category',values=('Payment'),aggfunc='sum',margins=True,margins_name='Total')fig = tools.make_subplots(rows=2, cols=2, subplot_titles=('The first','3','2','4'))pie1=go.Pie(labels=con_pivot.index,values=con_pivot.values)fig.append_trace(pie1,1,1)pyo.plot(fig)

Any help help will be appreciated. Thank you

Answer

The way to achieve the side by side pie charts using the make_subplots function from plotly would be the following (Many thanks to @Oysiyl for the input data):

from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly.offline import plotfig = make_subplots(rows=1, cols=2, specs=[[{"type": "pie"}, {"type": "pie"}]])fig.add_trace(go.Pie(values=[16, 15, 12, 6, 5, 4, 42],labels=["US", "China", "European Union", "Russian Federation","Brazil", "India", "Rest of World"],domain=dict(x=[0, 0.5]),name="GHG Emissions"), row=1, col=1)fig.add_trace(go.Pie(values=[27, 11, 25, 8, 1, 3, 25],labels=["US", "China", "European Union", "Russian Federation","Brazil", "India", "Rest of World"],domain=dict(x=[0.5, 1.0]),name="CO2 Emissions"),row=1, col=2)plot(fig)

enter image description here

https://en.xdnf.cn/q/71131.html

Related Q&A

Example of use \G in negative variable-length lookbehinds to limit how far back the lookbehind goes

In the pypi page of the awesome regex module (https://pypi.python.org/pypi/regex) it is stated that \G can be used "in negative variable-length lookbehinds to limit how far back the lookbehind goe…

Regex with lookbehind not working using re.match

The following python code:import reline="http://google.com" procLine = re.match(r(?<=http).*, line) if procLine.group() == "":print(line + ": did not match regex") els…

testing python multiprocessing pool code with nose

I am trying to write tests with nose that get set up with something calculated using multiprocessing.I have this directory structure:code/tests/tests.pytests.py looks like this:import multiprocessing a…

Python verify url goes to a page

I have a list of urls (1000+) which have been stored for over a year now. I want to run through and verify them all to see if they still exist. What is the best / quickest way to check them all and re…

Bokeh: Synchronizing hover tooltips in linked plots

I have two linked plots. When hovering, I would like to have a tooltip appear in both plots. I already use the linked selection with great success, but now I want to link the tooltips also.Below is an …

Pipe STDIN to a script that is itself being piped to the Python interpreter?

I need to implement an SVN pre-commit hook which executes a script that itself is stored in SVN.I can use the svn cat command to pipe that script to the Python interpreter, as follows:svn cat file://$R…

subprocess.call using cygwin instead of cmd on Windows

Im programming on Windows 7 and in one of my Python projects I need to call bedtools, which only works with Cygwin on Windows. Im new to Cygwin, installed the default version + everything needed for be…

Django Celery Received unregistered task of type appname.tasks.add

Following the documentation and the Demo Django project here https://github.com/celery/celery/tree/3.1/examples/djangoProject Structurepiesup2|piesup2| |__init__.py| |celery.py| |settings.py| |urls…

Documenting and detailing a single script based on the comments inside

I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like: # -*- coding: …

Using Ansible variables in testinfra

Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running teststest.pyimport pytest def test_zabbix_agent_package(host):package = host.pa…