基于 "count of" 列绘制直方图连续颜色?
Plotly histogram continuous colors based on "count of" column?
我创建了一个直方图,显示每十年(德语中的“Jahrzehnt”)温度高于 35 摄氏度的天数。我想根据炎热天数(这里是“温度,最大值”列)给它上色,但是正如你在图片中看到的,最热的十年(2010 年)的颜色变暗了,尽管我希望它是深色的红色.
我的密码是
fig = px.histogram(grouper, x='Jahrzehnt', y='Temperatur, Maximum', color='Temperatur, Maximum', \
color_discrete_sequence=px.colors.sequential.Reds,
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False)
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title='Anzahl')
如何更改 color_discrete_sequence 以使其匹配最低值和最高值以及从昏暗到饱和的红色?
感谢您的任何提示。
- 使用 https://plotly.com/python-api-reference/generated/plotly.express.colors.html
sample_colorscale()
您可以从连续映射构建离散序列
- 使用了列表理解,并将颜色更改为x轴而不是y轴列
- 完整代码如下
import pandas as pd
import numpy as np
import plotly.express as px
grouper = pd.DataFrame(
{
"Jahrzehnt": range(1870, 2011, 10),
"Temperatur, Maximum": np.random.randint(2, 40, 15),
}
)
grouper["Jahrzehnt"] = grouper["Jahrzehnt"].astype(str) + "er"
fig = px.histogram(
grouper,
x="Jahrzehnt",
y="Temperatur, Maximum",
color="Jahrzehnt",
color_discrete_sequence=[
px.colors.sample_colorscale("reds", v)[0]
for v in (
grouper["Temperatur, Maximum"] / grouper["Temperatur, Maximum"].max()
).tolist()
],
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False),
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title="Anzahl")
谢谢,我通过使用条形图而不是直方图找到了另一个答案,它允许使用连续色标按预期着色,如您的图所示,Rob:
def wueste(df):
tag = df[df['Temperatur, Maximum'] >= 35]
grouper = tag.groupby(['Jahrzehnt'])['Temperatur, Maximum'].count()\
.to_frame()\
.reset_index()\
grouper = grouper[grouper['Temperatur, Maximum'] !=0]
fig = px.bar(grouper, x='Jahrzehnt', y='Temperatur, Maximum', color='Temperatur, Maximum', \
color_continuous_scale='reds',
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False)
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title='Anzahl')
return fig
我创建了一个直方图,显示每十年(德语中的“Jahrzehnt”)温度高于 35 摄氏度的天数。我想根据炎热天数(这里是“温度,最大值”列)给它上色,但是正如你在图片中看到的,最热的十年(2010 年)的颜色变暗了,尽管我希望它是深色的红色.
我的密码是
fig = px.histogram(grouper, x='Jahrzehnt', y='Temperatur, Maximum', color='Temperatur, Maximum', \
color_discrete_sequence=px.colors.sequential.Reds,
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False)
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title='Anzahl')
如何更改 color_discrete_sequence 以使其匹配最低值和最高值以及从昏暗到饱和的红色?
感谢您的任何提示。
- 使用 https://plotly.com/python-api-reference/generated/plotly.express.colors.html
sample_colorscale()
您可以从连续映射构建离散序列 - 使用了列表理解,并将颜色更改为x轴而不是y轴列
- 完整代码如下
import pandas as pd
import numpy as np
import plotly.express as px
grouper = pd.DataFrame(
{
"Jahrzehnt": range(1870, 2011, 10),
"Temperatur, Maximum": np.random.randint(2, 40, 15),
}
)
grouper["Jahrzehnt"] = grouper["Jahrzehnt"].astype(str) + "er"
fig = px.histogram(
grouper,
x="Jahrzehnt",
y="Temperatur, Maximum",
color="Jahrzehnt",
color_discrete_sequence=[
px.colors.sample_colorscale("reds", v)[0]
for v in (
grouper["Temperatur, Maximum"] / grouper["Temperatur, Maximum"].max()
).tolist()
],
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False),
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title="Anzahl")
谢谢,我通过使用条形图而不是直方图找到了另一个答案,它允许使用连续色标按预期着色,如您的图所示,Rob:
def wueste(df):
tag = df[df['Temperatur, Maximum'] >= 35]
grouper = tag.groupby(['Jahrzehnt'])['Temperatur, Maximum'].count()\
.to_frame()\
.reset_index()\
grouper = grouper[grouper['Temperatur, Maximum'] !=0]
fig = px.bar(grouper, x='Jahrzehnt', y='Temperatur, Maximum', color='Temperatur, Maximum', \
color_continuous_scale='reds',
title='Tage über 35° Celsius ("Wüstentage")',
hover_data=dict(Jahrzehnt=False)
)
fig.update_layout(showlegend=False)
fig.update_yaxes(title='Anzahl')
return fig