상자 그림에 대한 범례 만 표시하는 서브 플롯 그래프를 만들었지 만 문제는 범례 항목이 '트레이스 3', '트레이스 4'및 '트레이스 5'로 나타나는데별로 도움이되지 않는다는 것입니다.
둘 다 작동하지 않기 때문에 코드 내에서 category_orders 또는 name 명령을 사용할 수 없습니다.
내 코드는 다음과 같습니다.
import random
import numpy as np
from plotly.subplots import make_subplots
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame({"bedrooms": random.choices(range(1,10), k=200),
"price": np.random.normal(loc = 500000, scale = 100000, size = 200).astype(int),
"sqft_living": np.random.normal(loc = 2000, scale = 500, size = 200).astype(int)})
df["sqft_living_bins"] = pd.cut(df["sqft_living"], bins = [0, 1500, 2500, 6000],
labels = ["Small", "Medium", "Large"])
fig = make_subplots(cols = 2, rows = 2,
subplot_titles = ("Price ($)", "Sq Ft Living", "Number of Bedrooms", "All 3"))
fig.add_trace(go.Histogram(x = df["price"], showlegend = False), row = 1, col = 1)
fig.add_trace(go.Histogram(x = df["sqft_living"], showlegend = False), row = 1, col = 2)
fig.add_trace(go.Histogram(x = df["bedrooms"], showlegend = False), row = 2, col = 1)
for i, sqft in enumerate(df["sqft_living_bins"].unique()):
df_plot = df[df["sqft_living_bins"] == sqft]
fig.add_trace(go.Box(x = df_plot["bedrooms"], y= df_plot["price"],
notched = True
#line = dict(color = colors[i]),
#name = "Small"
), row = 2, col = 2)
fig.update_layout(boxmode = "group",
legend = dict(orientation = "h",
yanchor = "bottom",
xanchor = "right",
y = -0.13,
x = 1))
fig.show()
아래 그래프에서 범례 항목을 'Small', 'Medium'및 'Large'로 바꾸고 싶습니다. 어떤 도움이라도 대단히 감사하겠습니다.
색상 사양과 마찬가지로 이름은 목록에서 반복 프로세스의 대상이 될 수 있습니다.
import random
import numpy as np
from plotly.subplots import make_subplots
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame({"bedrooms": random.choices(range(1,10), k=200),
"price": np.random.normal(loc = 500000, scale = 100000, size = 200).astype(int),
"sqft_living": np.random.normal(loc = 2000, scale = 500, size = 200).astype(int)})
df["sqft_living_bins"] = pd.cut(df["sqft_living"], bins = [0, 1500, 2500, 6000],
labels = ["Small", "Medium", "Large"])
fig = make_subplots(cols = 2, rows = 2,
subplot_titles = ("Price ($)", "Sq Ft Living", "Number of Bedrooms", "All 3"))
fig.add_trace(go.Histogram(x = df["price"], showlegend = False), row = 1, col = 1)
fig.add_trace(go.Histogram(x = df["sqft_living"], showlegend = False), row = 1, col = 2)
fig.add_trace(go.Histogram(x = df["bedrooms"], showlegend = False), row = 2, col = 1)
colors = ['orange','blue','green']
names = ['Large','Medium','Small']
for i, sqft in enumerate(df["sqft_living_bins"].unique()):
df_plot = df[df["sqft_living_bins"] == sqft]
fig.add_trace(go.Box(x = df_plot["bedrooms"], y= df_plot["price"],
notched = True,
line = dict(color = colors[i]),
name = names[i]
), row = 2, col = 2)
fig.update_layout(boxmode = "group",
legend = dict(orientation = "h",
yanchor = "bottom",
xanchor = "right",
y = -0.13,
x = 1))
fig.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다