我有以下图表,其数据(位置和颜色值)来自外部来源:
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
data = go.Scatter({
'hoverinfo': 'text',
'marker': {'color': ['rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(30,136,229,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(30,136,229,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(101,103,181,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(101,103,181,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(101,103,181,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(101,103,181,1.0)', 'rgba(245,39,87,1.0)', 'rgba(245,39,87,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(173,71,134,1.0)', 'rgba(101,103,181,1.0)', 'rgba(173,71,134,1.0)', 'rgba(101,103,181,1.0)', 'rgba(173,71,134,1.0)', 'rgba(245,39,87,1.0)', 'rgba(101,103,181,1.0)', 'rgba(101,103,181,1.0)', 'rgba(245,39,87,1.0)', …Run Code Online (Sandbox Code Playgroud) 我想使用create_gantt在python中创建甘特图。但是,当开始日期和结束日期重叠时,我会遇到问题。
例如:
import plotly.plotly as py
import plotly.figure_factory as ff
import plotly
df = [dict(Task="Milestone A", Start='2017-01-01', Finish='2017-02-02', Resource='Jack'),
dict(Task="Milestone B", Start='2018-01-01', Finish='2018-02-02', Resource='Jack'),
dict(Task="Milestone A", Start='2017-01-17', Finish='2017-04-28', Resource='Joe'),
dict(Task="Milestone B", Start='2017-03-17', Finish='2017-04-28', Resource='Joe'),
dict(Task="Milestone A", Start='2017-01-14', Finish='2017-03-14', Resource='John'),
dict(Task="Milestone B", Start='2018-01-14', Finish='2018-03-14', Resource='John')]
colors = {'Jack': 'rgb(220, 0, 0)',
'Joe': (1, 0.9, 0.16),
'John': 'rgb(0, 255, 100)'}
fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True, group_tasks=True)
plotly.offline.plot(fig, filename='gantt-group-tasks-together')
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,图中的里程碑A和里程碑B的Joe,Jack和John的线条重叠。输出不良 我想看到乔,约翰和杰克的里程碑A的3行聚集在一起,但不像这样重叠
我该如何实现?良好的输出
我用 Python 创建了一个简单的 Plotly 图表,并且可以非常轻松地更改模式栏选项。但是,当我尝试将相同的图表发布到 Chart-Studio 时,并非所有模式栏配置都有效。有任何想法吗?
这是代码:
import pandas as pd
import numpy as np
import plotly.graph_objects as go
df = pd.DataFrame({'a':[2,4,6,8,10],'b':[3,6,9,12,15]})
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.index, y=df['a'], mode='lines', name='a', line=dict(color='dodgerblue', dash='solid')))
fig.add_trace(go.Scatter(x=df.index, y=df['b'], mode='lines', name='b', line=dict(color='lightsalmon', dash='dash')))
fig.update_layout(
title='a and b',
autosize=False,
width=500,
height=500,
newshape=dict(line_color='mediumaquamarine', line_width=2, opacity=1)
)
config = {
'modeBarButtonsToAdd' : ['drawline', 'drawopenpath', 'drawrect', 'eraseshape'],
'displaylogo' : False
}
fig.show(config=config)
Run Code Online (Sandbox Code Playgroud)
出现的图形已删除徽标,并且模式栏上的附加按钮按其应有的方式显示...
但是,如果我尝试将其发布到 Chart-Studio:
import chart_studio.plotly as py
import chart_studio
chart_studio.tools.set_credentials_file(username='xxxxx', api_key='#####')
py.plot(fig, filename='a and b', auto_open=False, config=config) …Run Code Online (Sandbox Code Playgroud) 我想更改plotly.express条形图图例中项目的顺序。例如,我想在此图上显示“午餐前的晚餐”(当前的行为对于水平条形图尤其尴尬,因为条形的顺序与图例顺序相反):
import plotly.express as px
df = px.data.tips()
# Sort to put dinner on top.
df.sort_values('time', ascending=False, inplace=True)
fig = px.bar(df, y='sex', x='total_bill', color='time', barmode='group',
orientation='h')
fig.update_layout(yaxis={'categoryorder': 'total ascending'})
fig.show()
Run Code Online (Sandbox Code Playgroud)
我想在情节的各个方面添加痕迹。
例如,我想向显示 15% 小费的“tips”数据集散点图的每个每日方面添加一条参考线。然而,我下面的尝试仅将线添加到第一个方面。
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
df = px.data.tips()
ref_line_slope = 0.15 # 15% tip for reference
ref_line_x_range = np.array([df.total_bill.min(), df.total_bill.max()])
fig = px.scatter(df, x="total_bill", y="tip",facet_col="day", trendline='ols')
fig = fig.add_trace(go.Scatter(x=reference_line_x_range,y=ref_line_slope*reference_line_x_range,name='15%'))
fig.show()
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用绘图来比较使用误差线作为置信区间的回归模型的系数。我使用以下代码来绘制它,使用变量作为y散点图中的分类轴。问题是这些点是重叠的,我想像设置时在条形图中发生的那样避开它们barmode='group'。如果我有一个数字轴,我可以手动躲避它们,但我做不到。
fig = px.scatter(
df, y='index', x='coef', text='label', color='model',
error_x_minus='lerr', error_x='uerr',
hover_data=['coef', 'pvalue', 'lower', 'upper']
)
fig.update_traces(textposition='top center')
fig.update_yaxes(autorange="reversed")
Run Code Online (Sandbox Code Playgroud)
使用构面,我几乎得到了我想要的结果,但有些标签超出了绘图范围并且不可见:
fig = px.scatter(
df, y='model', x='coef', text='label', color='model',
facet_row='index',
error_x_minus='lerr', error_x='uerr',
hover_data=['coef', 'pvalue', 'lower', 'upper']
)
fig.update_traces(textposition='top center')
fig.update_yaxes(visible=False)
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
Run Code Online (Sandbox Code Playgroud)
有人对于在第一种情况下躲避点或在第二种情况下显示标签有任何想法或解决方法吗?
提前致谢。
PS:这是我为生成绘图而制作的随机假数据框:
df = pd.DataFrame({'coef': {0: 1.0018729737113143,
1: 0.9408864645423858,
2: 0.29796556981484884,
3: -0.6844053575764955,
4: -0.13689631932690113,
5: 0.1473096200402363,
6: 0.9564712505670716,
7: 0.956099003887811,
8: 0.33319108930207175,
9: -0.7022778825729681,
10: -0.1773916842612131,
11: 0.09485417304851751},
'index': {0: 'const',
1: …Run Code Online (Sandbox Code Playgroud) 我想用 plotly (python) 向现有图形添加箭袋。但是我能找到的唯一和平的文件要么只创建一个箭袋(这里)或一个全新的人物(那里)。
这是 plotly doc 的示例:
import plotly.figure_factory as ff
import numpy as np
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
fig = ff.create_quiver(x, y, u, v)
fig.show()
Run Code Online (Sandbox Code Playgroud)
如果有人对我所做的情节有更好的理解,我会很感激一些解释!
非常感谢,
我试图在 Plotly 中突出显示时间段。我似乎最好的方法是使用 Shapes,就像这样,但在现实世界中,您不想像示例 url 中那样手动添加每个形状。我认为 for 循环是最好的解决方案,但欢迎其他(计算量较小的)建议。
我的脚本如下所示:
np.random.seed(12345)
rows = 20
x = pd.Series(np.random.randn(rows),index=pd.date_range('1/1/2020', periods=rows)).cumsum()
df = pd.DataFrame({"index": x})
# add column showing what to shade
df["signal"] = df['index'] < 5
# plot index and highlight periods with Rectangle Shapes in Plotly
fig = px.line(df, x=df.index, y="index")
for row in df.iterrows():
if df['signal'] == False:
ply_shapes['shape_' + str(i)]=go.layout.Shape(type="rect",
x0=df.dato[i-1],
y0=0,
x1=df.dato[i],
y1=2000,
opacity=0.5,
layer="below"
)
lst_shapes=list(ply_shapes.values())
fig.update_layout(shapes=lst_shapes)
fig.show()
Run Code Online (Sandbox Code Playgroud)
但这返回:
ValueError: The truth value of a Series …Run Code Online (Sandbox Code Playgroud) 我想在几个子图上显示额外的数据,并决定在子图标题中这样做。
我已经弄清楚如何向子图添加标题,但无法在每个子图中包含变量。到目前为止的代码是:
fig = make_subplots(rows=3, cols=1, subplot_titles=("Share Price is: ", "RSI is: ", "Portfolio Return is: "))
Run Code Online (Sandbox Code Playgroud)
我想在每个子图标题的末尾添加变量。
如何才能做到这一点?
我正在尝试在具有子图的绘图中创建与不同日期范围相对应的阴影区域。
理想情况下,我希望每个阴影矩形都适合每个子图,但我发现这很困难。这是一些示例代码:
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = pd.DataFrame({'A': list(range(25)),
'B': list(range(25, 50)),
'C': list(range(50, 75))}, index=pd.date_range('20200101', periods=25))
fig = make_subplots(rows=3, cols=1)
for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, y=df[col]), row=idx + 1, col=1)
shape_dict = {'type':'rect', 'xref':'x', 'yref':'paper', 'x0':'2020-01-03', 'x1':'2020-01-12', 'y0':0, 'y1':1, 'fillcolor': 'LightSalmon', 'layer': 'below', 'opacity': 0.25, 'line_width': 0}
Run Code Online (Sandbox Code Playgroud)
如果我这样做fig.update_layout(shapes=[shape_dict]),那么我会得到这个:
还不错,但我更喜欢将这些形状中的每一个单独安装到它们自己的子图中。
当我尝试使用 执行此操作时add_shape,阴影区域会失去缩放比例:
for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, …Run Code Online (Sandbox Code Playgroud) plotly-python ×10
python ×10
plotly ×9
pandas ×2
facet ×1
gantt-chart ×1
matplotlib ×1
plot ×1
plotly-dash ×1
python-3.x ×1
subplot ×1