Plotly:如何使用 space 为文本模板自动化 y 轴?
Plotly: How to automate y-axis with space for texttemplate?
我想弄清楚如何使用文本模板绘制此条形图。不切割它们,但也保持 y 轴动态。感谢您的想法!
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
fig.update_layout(uniformtext_minsize=20, uniformtext_mode='show')
plotly.io.write_image(fig, file='bartest.png', format='png')
设置uniformtext_minsize=20
会让你的身材看起来很拥挤。所以我实际上会选择 fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
而不是:
fig.update_traces(texttemplate='%{text:.2%}', textposition='inside')
fig.update_layout(uniformtext_minsize=12, uniformtext_mode='show')
情节 1
Plotly 将 yaxis 的大小调整为条形的大小,而不是之后添加到其中的文本,这样您就可以防止出现任何切断文本的问题。如果您出于某种原因更喜欢将文本元素保留在条形图上方,则必须相应地调整 y-aixs,例如:
fig.update_yaxes(range=[0, max(df.lifeExp)*1.2])
但是如您所见,您必须将 uniformtext_minsize
设置为一个不切实际的低数字才能使文本元素可读:
情节 2
情节 1 的完整代码
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='inside')
fig.update_layout(uniformtext_minsize=11, uniformtext_mode='show')
# plotly.io.write_image(fig, file='bartest.png', format='png')
fig.show()
情节 2 的完整代码
将 plotly.express 导入为 px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
fig.update_layout(uniformtext_minsize=5, uniformtext_mode='show')
# plotly.io.write_image(fig, file='bartest.png', format='png')
fig.update_yaxes(range=[0, max(df.lifeExp)*1.2])
fig.show()
我想弄清楚如何使用文本模板绘制此条形图。不切割它们,但也保持 y 轴动态。感谢您的想法!
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
fig.update_layout(uniformtext_minsize=20, uniformtext_mode='show')
plotly.io.write_image(fig, file='bartest.png', format='png')
设置uniformtext_minsize=20
会让你的身材看起来很拥挤。所以我实际上会选择 fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
而不是:
fig.update_traces(texttemplate='%{text:.2%}', textposition='inside')
fig.update_layout(uniformtext_minsize=12, uniformtext_mode='show')
情节 1
Plotly 将 yaxis 的大小调整为条形的大小,而不是之后添加到其中的文本,这样您就可以防止出现任何切断文本的问题。如果您出于某种原因更喜欢将文本元素保留在条形图上方,则必须相应地调整 y-aixs,例如:
fig.update_yaxes(range=[0, max(df.lifeExp)*1.2])
但是如您所见,您必须将 uniformtext_minsize
设置为一个不切实际的低数字才能使文本元素可读:
情节 2
情节 1 的完整代码
import plotly.express as px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='inside')
fig.update_layout(uniformtext_minsize=11, uniformtext_mode='show')
# plotly.io.write_image(fig, file='bartest.png', format='png')
fig.show()
情节 2 的完整代码
将 plotly.express 导入为 px
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
df.lifeExp = df.lifeExp/100
fig = px.bar(df, y='lifeExp', x='country', text='lifeExp')
fig.update_traces(texttemplate='%{text:.2%}', textposition='outside')
fig.update_layout(uniformtext_minsize=5, uniformtext_mode='show')
# plotly.io.write_image(fig, file='bartest.png', format='png')
fig.update_yaxes(range=[0, max(df.lifeExp)*1.2])
fig.show()