如果这可能是重复的,我深表歉意。这是我的数据:
import pandas as pd
data = [
{'Year': 2015, 'major': None, 'minor': 57.0, 'patch': None},
{'Year': 2016, 'major': 129.0, 'minor': 107.0, 'patch': 437.0},
{'Year': 2017, 'major': 109.0, 'minor': 266.0, 'patch': 2622.0},
{'Year': 2018, 'major': 61.0, 'minor': 3213.0, 'patch': 2989.0},
{'Year': 2019, 'major': 2906.0, 'minor': 9780.0, 'patch': 16531.0},
{'Year': 2020, 'major': 421.0, 'minor': 8447.0, 'patch': 9402.0},
{'Year': 2021, 'major': 1156.0, 'minor': 13102.0, 'patch': 10323.0},
{'Year': 2022, 'major': 246.0, 'minor': 8770.0, 'patch': 24588.0}
]
df2 = pd.DataFrame(data)
我正在尝试绘制所有三个版本的计数,但由于某种原因,
nan
不断出现,这也只针对 major
。有什么办法可以解决这个问题吗?这是我的代码:
fig = go.Figure()
fig.add_trace(go.Scatter(x=df2['Year'], y=df2['major'], name='Major', mode='lines+markers', marker=dict(color='brown', size=10), line=dict(color='brown', width=1)))
fig.add_trace(go.Scatter(x=df2['Year'], y=df2['minor'], name='Minor', mode='lines+markers', marker=dict(color='orange', size=10), line=dict(color='orange', width=1)))
fig.add_trace(go.Scatter(x=df2['Year'], y=df2['patch'], name='Patch', mode='lines+markers', marker=dict(color='blue', size=10), line=dict(color='blue', width=1)))
for row in df2.itertuples():
fig.add_annotation(x=row.Year, y=row.major, text=str(row.major), showarrow=False, arrowhead=1, yshift=10)
fig.add_annotation(x=row.Year, y=row.minor, text=str(row.minor), showarrow=False, arrowhead=1, yshift=20)
fig.add_annotation(x=row.Year, y=row.patch, text=str(row.patch), showarrow=False, arrowhead=1, yshift=10)
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_layout(width=1500, height=900,legend_title_text='Type of Change',template='ggplot2', title = 'Evolution of Breaking Changes over all Releases', xaxis_title = 'Year', yaxis_title = 'Number of Breaking Changes')
fig.update_traces(mode='lines+markers')
fig.show()
如您所见,2015 年的注释出现在 major 上,但没有出现在 patch 上,我不确定为什么会这样。任何帮助将不胜感激。
我认为这是因为 plotly 处理注释文本的方式与处理跟踪的方式不同。从某种意义上说,Plotly traces 是“更聪明的”,因为它们通常会从显示的许多图表类型中删除
NaN
值。
在这种情况下,我认为使用
mode='lines+markers+text'
创建散点图并将 y 值作为文本传递会简化您的问题并完全避免使用注释。 go.Scatter
将通过删除标记和文本来处理 NaN
。
例如:
import pandas as pd
import plotly.graph_objects as go
data = [
{'Year': 2015, 'major': None, 'minor': 57.0, 'patch': None},
{'Year': 2016, 'major': 129.0, 'minor': 107.0, 'patch': 437.0},
{'Year': 2017, 'major': 109.0, 'minor': 266.0, 'patch': 2622.0},
{'Year': 2018, 'major': 61.0, 'minor': 3213.0, 'patch': 2989.0},
{'Year': 2019, 'major': 2906.0, 'minor': 9780.0, 'patch': 16531.0},
{'Year': 2020, 'major': 421.0, 'minor': 8447.0, 'patch': 9402.0},
{'Year': 2021, 'major': 1156.0, 'minor': 13102.0, 'patch': 10323.0},
{'Year': 2022, 'major': 246.0, 'minor': 8770.0, 'patch': 24588.0}
]
df2 = pd.DataFrame(data)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df2['Year'], y=df2['major'],
text=df2['major'], textposition="top center",
name='Major', mode='lines+markers+text',
marker=dict(color='brown', size=10), line=dict(color='brown', width=1)
))
fig.add_trace(go.Scatter(
x=df2['Year'], y=df2['minor'],
text=df2['minor'], textposition="top center",
name='Minor', mode='lines+markers+text',
marker=dict(color='orange', size=10), line=dict(color='orange', width=1)
))
fig.add_trace(go.Scatter(
x=df2['Year'], y=df2['patch'],
text=df2['patch'], textposition="top center",
name='Patch', mode='lines+markers+text',
marker=dict(color='blue', size=10), line=dict(color='blue', width=1)
))
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_layout(width=1500, height=900,legend_title_text='Type of Change',template='ggplot2', title = 'Evolution of Breaking Changes over all Releases', xaxis_title = 'Year', yaxis_title = 'Number of Breaking Changes')
fig.update_traces(mode='lines+markers+text')
fig.show()