gpt4 book ai didi

python - 有没有办法使用plotly在python中创建范围图?

转载 作者:行者123 更新时间:2023-12-03 08:38:47 25 4
gpt4 key购买 nike

我能够创建一个简单的点图,但需要一种方法来连接这些点。
enter image description here

最佳答案

我稍微编辑了 @r-beginners code为了将颜色渐变添加到线条、注释及其颜色。关于颜色渐变,如曝光here :

This feature is not yet available for 2D line plots, it is currentlyonly available for 3D line plots, seehttps://github.com/plotly/plotly.js/issues/581. It is however possibleto use a colorscale in a 2D plot if you use markers instead of lines,see the example below.

这是我的代码:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

df = pd.DataFrame({'Grade': ["Doctorate Degree",
"Master's Degree",
"Bachelor's Degree",
"Associate Degree",
"Some college, no degree",
"Hight School",
"9th 12th(no degree)",
"Less Than 9th Grade"],
'Women': [91, 69, 54, 38, 35, 29, 22, 21],
'Men': [151, 99, 84, 56, 53, 47, 37, 34]})

w_lbl = list(map(lambda x: str(x), df['Women']))
m_lbl = list(map(lambda x: str(x), df['Men']))

fig = go.Figure()

for i in range(0, len(df)):

fig.add_trace(go.Scatter(x = np.linspace(df['Women'][i], df['Men'][i], 1000),
y = 1000*[df['Grade'][i]],
mode = 'markers',
marker = {'color': np.linspace(df['Women'][i], df['Men'][i], 1000),
'colorscale': ['#E1A980', '#8DAEA6'],
'size': 8}))

fig.add_trace(go.Scatter(x = df['Women'],
y = df['Grade'],
marker = dict(color = '#CC5600', size = 14),
mode = 'markers+text',
text = w_lbl,
textposition = 'middle left',
textfont = {'color': '#CC5600'},
name = 'Woman'))

fig.add_trace(go.Scatter(x = df['Men'],
y = df['Grade'],
marker = dict(color = '#237266', size = 14),
mode = 'markers+text',
text = m_lbl,
textposition = 'middle right',
textfont = {'color': '#237266'},
name = 'Men'))

fig.add_annotation(x = df.iloc[-1, df.columns.get_loc('Women')] - 5,
y = df.iloc[-1, df.columns.get_loc('Grade')],
text = 'Women',
font = {'color': '#CC5600',
'size': 15},
showarrow = False)

fig.add_annotation(x = df.iloc[-1, df.columns.get_loc('Men')] + 5,
y = df.iloc[-1, df.columns.get_loc('Grade')],
text = 'Men',
font = {'color': '#237266',
'size': 15},
showarrow = False)

fig.update_layout(title = "Sample plotly",
showlegend = False)

fig.show()

剧情:

enter image description here

关于python - 有没有办法使用plotly在python中创建范围图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63213527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com