如何使用 plotly 将 pandas Dataframe 绘制为饼图
How to plot pandas Dataframe as a Pie chart using plotly
我能够使用 pandas 找到计数和百分比。但是当我尝试 plaot 时,我只看到一个空的 HTML 页面。请协助将以下数据框绘制为饼图。
Python代码
import pandas as pd
import plotly.graph_objects as go
import plotly.offline as py
df = pd.read_excel('data/master.xlsx')
#Commercials ISTQB foundation data
df_commercials=df[(df['Domain']=='Commercials')]
ds=df_commercials['ISTQB Foundation']
count = ds.value_counts()
print(count)
print()
print("=================================================")
percent = ds.value_counts(normalize=True)
istqbF_series = round((percent*100),2).astype(str)+'%'
istqbF_df = pd.DataFrame({'ISTQB Foundation':istqbF_series.index,'percentage':istqbF_series.values})
print(istqbF_df)
print()
labels = istqbF_df['ISTQB Foundation']
values = istqbF_df['percentage']
# trace = go.Pie(labels=labels, values=values)
# fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
#
#
# py.plot(fig, filename='ISTQB_Foundation_pie_chart.html')
输出:
我想以饼图的形式绘制输出,使用(ISTQB Foundation vs percentage)悬停时显示计数。
我尝试在网上搜索了好几天,但最终找不到指导我进行此操作的教程。非常感谢任何帮助
这里您可能会遇到错误,因为您的两列都是分类的(字符串)。尝试如下
import plotly.offline as py
from plotly.graph_objs import Pie, Layout,Figure
percent = ds.value_counts(normalize=True).mul(100).round(2)
_layout = Layout(title='ISTQB_Foundation')
_data = Pie(labels=percent.index.tolist(),values=percent.values.tolist(),hoverinfo='label+percent')
fig = Figure(data=[_data], layout=_layout)
# save html file to local
py.plot(fig,filename='ISTQB_Foundation_pie_chart.html')
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
我能够使用 pandas 找到计数和百分比。但是当我尝试 plaot 时,我只看到一个空的 HTML 页面。请协助将以下数据框绘制为饼图。
Python代码
import pandas as pd
import plotly.graph_objects as go
import plotly.offline as py
df = pd.read_excel('data/master.xlsx')
#Commercials ISTQB foundation data
df_commercials=df[(df['Domain']=='Commercials')]
ds=df_commercials['ISTQB Foundation']
count = ds.value_counts()
print(count)
print()
print("=================================================")
percent = ds.value_counts(normalize=True)
istqbF_series = round((percent*100),2).astype(str)+'%'
istqbF_df = pd.DataFrame({'ISTQB Foundation':istqbF_series.index,'percentage':istqbF_series.values})
print(istqbF_df)
print()
labels = istqbF_df['ISTQB Foundation']
values = istqbF_df['percentage']
# trace = go.Pie(labels=labels, values=values)
# fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
#
#
# py.plot(fig, filename='ISTQB_Foundation_pie_chart.html')
输出:
我想以饼图的形式绘制输出,使用(ISTQB Foundation vs percentage)悬停时显示计数。
我尝试在网上搜索了好几天,但最终找不到指导我进行此操作的教程。非常感谢任何帮助
这里您可能会遇到错误,因为您的两列都是分类的(字符串)。尝试如下
import plotly.offline as py
from plotly.graph_objs import Pie, Layout,Figure
percent = ds.value_counts(normalize=True).mul(100).round(2)
_layout = Layout(title='ISTQB_Foundation')
_data = Pie(labels=percent.index.tolist(),values=percent.values.tolist(),hoverinfo='label+percent')
fig = Figure(data=[_data], layout=_layout)
# save html file to local
py.plot(fig,filename='ISTQB_Foundation_pie_chart.html')
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER