Python 如何阻止Plotly Scatter创建不存在的X值
我试着用谷歌搜索了一下,并实现了我发现的所有假定的修复:设置Python 如何阻止Plotly Scatter创建不存在的X值,python,python-3.x,plotly,Python,Python 3.x,Plotly,我试着用谷歌搜索了一下,并实现了我发现的所有假定的修复:设置tickvals和connectgaps,但没有解决问题 我有一个大约100行的数据集,其中包含1998年、1999年和2020年的房地产交易值。然而,Plotly坚持在1999年到2020年之间每年创建一次,即使数据集中不存在 以下是我的图表代码: def scatter(df): """ Must take in a df with property_type, year, price_
tickvals
和connectgaps
,但没有解决问题
我有一个大约100行的数据集,其中包含1998年、1999年和2020年的房地产交易值。然而,Plotly坚持在1999年到2020年之间每年创建一次,即使数据集中不存在
以下是我的图表代码:
def scatter(df):
"""
Must take in a df with property_type, year, price_paid
"""
figs = []
for prop_type in df['property_type'].unique():
df_filt = df[df['property_type'] == prop_type]
figs.append(
go.Scatter(x=df_filt['year'],
y=df_filt['price_paid'],
mode='markers',
marker=dict(
size=15,
opacity=0.5,
line=dict(
width=1,
color='darkSlateGrey'
)
),
name=prop_type,
connectgaps=True)
)
layout = go.Layout(width=1000,
height=600)
fig = go.Figure(data=figs, layout=layout)
fig.update_xaxes(tickmode='array',
tickvals=[n for n in range(len(df_filt['year'].unique()))],
ticktext=[n for n in df_filt['year'].unique()])
chart = pyo.plot(fig, output_type='div', include_plotlyjs=True)
return chart
然而,这就是它所显示的
感谢大家的帮助
谢谢解决方案可能(根据您的评论,肯定是)非常简单,只需将以下内容添加到xaxis
布局中即可:
'type': 'category'
您是否尝试过设置xaxis
“类型”:“类别”
?谢谢,是的!这已经奏效了。编辑:我如何选择你的评论作为答案?太好了,很高兴它帮助了你!我刚刚给你提出了一个正确的答案。干杯,伙计,欢迎!干杯