Ter*_*e.T 5 python matplotlib plotly
我正在使用waterfall_chartPython中的包创建瀑布图。该软件包主要用于matplotlib后端,因此我试图使用该tls.mpl_to_plotly(mpl_fig)功能将matplotlib图形隐藏到plotly。但是在转换时会弹出错误。有没有一种方法可以转换waterfall_chart成plotly该图表,或者有一种简单的方法可以直接在其中创建图表plotly?我看到类似的一些前面的讨论图表中plotly,但它涉及相当手册图表数量的编码。
您可以使用以下代码重新创建图表。
import waterfall_chart
import matplotlib.pyplot as plt
import plotly.tools as tls
a = ['sales','returns','credit fees','rebates','late charges','shipping']
b = [10,-30,-7.5,-25,95,-7]
mpl_fig = plt.figure()
waterfall_chart.plot(a, b)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试转换为plotlyusing时mpl_to_plotly(),出现错误:
plotly_fig = tls.mpl_to_plotly(mpl_fig)
ValueError: min() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)
该waterfall_chart软件包的详细信息可以在这里找到:https : //github.com/chrispaulca/waterfall/blob/master/waterfall_chart.py
我的回答地址
[...]或者有没有一种简单的方法可以直接在绘图中创建图表?
使用较新版本的plotly,您可以使用plotly.graph_objs.Waterfall.
iplot下面是一个基本示例,其中包含您的数据示例以及在离线 Jupyter Notebook 中使用的设置:
阴谋:
代码:
# imports
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from IPython.core.display import display, HTML
import plotly.figure_factory as ff
import plotly.graph_objs as go
import pandas as pd
import numpy as np
# setup
display(HTML("<style>.container { width:35% !important; } .widget-select > select {background-color: gainsboro;}</style>"))
init_notebook_mode(connected=True)
np.random.seed(1)
import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected = False)
# your values
a = ['sales','returns','credit fees','rebates','late charges','shipping']
b = [10,-30,-7.5,-25,95,-7]
# waterfall trace
trace = go.Waterfall(
x = a,
textposition = "outside",
text = [str(elem) for elem in b],
y = b,
connector = {"line":{"color":"rgb(63, 63, 63)"}},
)
layout = go.Layout(
title = "Waterfall chart, plotly version 3.9.0",
showlegend = True
)
iplot(go.Figure([trace], layout))
Run Code Online (Sandbox Code Playgroud)
检查您的版本:
import plotly
plotly.__version__
Run Code Online (Sandbox Code Playgroud)
使用以下命令在 cmd 控制台中更新您的版本:
pip install plotly --upgrade
Run Code Online (Sandbox Code Playgroud)