Python Plotly 서브 플롯 축 접두사
Aug 29 2020
줄거리 서브 플롯에서 x 축에 대한 접두사로 통화 기호를 원합니다. 명령은 다른 곳에서 작동하기 때문에 괜찮지 만 서브 플롯 기능과 통합 될 때 재생되는 것 같습니다.
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import random
x = np.array(["France", "Spain", "Italy", "Chile"])
df = pd.DataFrame({"country": np.repeat(x, [10,10,10,10]).tolist(),
"rating": random.sample(range(0,100),40),
"price": random.sample(range(100,1000),40)})
scatter = make_subplots(rows = 2, cols = 2, shared_yaxes = True, shared_xaxes = True,
subplot_titles = ("France", "Spain", "Italy", "Chile"),
x_title = "Price", y_title = "Rating"
)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="France", "price"],
y = df.loc[df["country"]=="France", "rating"],
mode = "markers"),
row = 1, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Spain", "price"],
y = df.loc[df["country"]=="Spain", "rating"],
mode = "markers"),
row = 1, col = 2)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Italy", "price"],
y = df.loc[df["country"]=="Italy", "rating"],
mode = "markers"),
row = 2, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Chile", "price"],
y = df.loc[df["country"]=="Chile", "rating"],
mode = "markers"),
row = 2, col = 2)
scatter.update_layout(showlegend = False, plot_bgcolor = "white",
xaxis = dict(showtickprefix = "all", tickprefix = "£"))
scatter.show()
공유 x 및 y 축 명령을 제거하면 통화가 왼쪽 하단 서브 플롯에만 표시되지만 실제로 제거하고 싶지는 않습니다.
누구든지 이것에 대해 어떤 방법을 알고 있습니까?
최신 정보
현재 다음 그래프를 만들고 있습니다.
다음을 만들 수 있기를 원합니다.
답변
2 rpanai Aug 29 2020 at 04:27
나는 이것이이 대답 과 매우 유사하다고 생각합니다 . 아이디어는 다음 for_each_xaxis과 for_each_yaxis같이 모든 단일 추적을 업데이트하는 것입니다 .
데이터
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import random
x = np.array(["France", "Spain", "Italy", "Chile"])
df = pd.DataFrame({"country": np.repeat(x, [10,10,10,10]).tolist(),
"rating": random.sample(range(0,100),40),
"price": random.sample(range(100,1000),40)})
음모
scatter = make_subplots(rows = 2, cols = 2,
shared_yaxes = True, shared_xaxes = True,
subplot_titles = ("France", "Spain", "Italy", "Chile"),
x_title = "Price", y_title = "Rating")
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="France", "price"],
y = df.loc[df["country"]=="France", "rating"],
mode = "markers"),
row = 1, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Spain", "price"],
y = df.loc[df["country"]=="Spain", "rating"],
mode = "markers"),
row = 1, col = 2)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Italy", "price"],
y = df.loc[df["country"]=="Italy", "rating"],
mode = "markers"),
row = 2, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Chile", "price"],
y = df.loc[df["country"]=="Chile", "rating"],
mode = "markers"),
row = 2, col = 2)
# New stuff from here
scatter = scatter.update_layout(showlegend = False, plot_bgcolor = "white")
def update_y(y):
y.update(matches=None)
y.showticklabels=True
def update_x(x):
x.update(matches=None)
x.showticklabels=True
x.tickprefix = "£"
scatter.for_each_yaxis(update_y)
scatter.for_each_xaxis(update_x)
사용 plotly.express
원하는 경우 결국 플롯 표현을 사용할 수 있지만이 경우 주석도 처리해야합니다.
import plotly.express as px
fig = px.scatter(
df,
x="price",
y="rating",
color="country",
facet_col="country",
facet_col_wrap=2,
facet_row_spacing=0.2, # default is 0.07 when facet_col_wrap is used
facet_col_spacing=0.04, # default is 0.03
)
fig = fig.update_layout(showlegend = False, plot_bgcolor = "white")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
def update_y(y):
y.update(matches=None)
y.showticklabels=True
y.title.text = ""
def update_x(x):
x.update(matches=None)
x.showticklabels=True
x.tickprefix = "£"
x.title.text = ""
fig.for_each_yaxis(update_y)
fig.for_each_xaxis(update_x)
extra_annotations =[
go.layout.Annotation(
{
'showarrow': False,
'text': 'Price',
'x': 0.5,
'xanchor': 'center',
'xref': 'paper',
'y': 0,
'yanchor': 'top',
'yref': 'paper',
'yshift': -30,
'font': dict(
# family="Courier New, monospace",
size=16,
# color="#ffffff"
),
}),
go.layout.Annotation(
{
'showarrow': False,
'text': 'Rating',
'x': 0,
'xanchor': 'center',
'xref': 'paper',
'y': 0.7,
'yanchor': 'top',
'yref': 'paper',
'xshift': -40,
'textangle': -90,
'font': dict(
# family="Courier New, monospace",
size=16,
# color="#ffffff"
),
})
]
annotations = list(fig.layout.annotations) + extra_annotations
fig.update_layout( annotations=annotations)
FluffySheep1990 Sep 03 2020 at 13:37
그래서 나는 그것을하는 방법을 알아 냈고, xaxisN_tickprefix = "£"'N'이 그래프의 플롯 번호 인 코드 로 개별적으로 서브 플롯을 업데이트해야합니다 . 이 경우 플롯 3과 4, 전체 코드와 그래프를 아래에서 업데이트하려고했습니다.
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import random
x = np.array(["France", "Spain", "Italy", "Chile"])
df = pd.DataFrame({"country": np.repeat(x, [10,10,10,10]).tolist(),
"rating": random.sample(range(0,100),40),
"price": random.sample(range(100,1000),40)})
scatter = make_subplots(rows = 2, cols = 2, shared_yaxes = True, shared_xaxes = True,
subplot_titles = ("France", "Spain", "Italy", "Chile"),
x_title = "Price", y_title = "Rating"
)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="France", "price"],
y = df.loc[df["country"]=="France", "rating"],
mode = "markers"),
row = 1, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Spain", "price"],
y = df.loc[df["country"]=="Spain", "rating"],
mode = "markers"),
row = 1, col = 2)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Italy", "price"],
y = df.loc[df["country"]=="Italy", "rating"],
mode = "markers"),
row = 2, col = 1)
scatter.add_trace(go.Scatter(x = df.loc[df["country"]=="Chile", "price"],
y = df.loc[df["country"]=="Chile", "rating"],
mode = "markers"),
row = 2, col = 2)
scatter.update_layout(showlegend = False, plot_bgcolor = "white",
#xaxis = dict(showtickprefix = "all", tickprefix = "£") <- old code
xaxis3_tickprefix = "£", xaxis4_tickprefix = "£") #new code
scatter.update_xaxes(range = [0,1000]) #also added to ensure the axis align
scatter.update_yaxes(range = [0,100]) #also added to ensure the axis align
scatter.show()