如何操纵行之间的间距(例如增加前两行和最后两行之间的间距)和图中图表的大小(例如使饼图更大)?
在我的示例中,我尝试使用plotly 的 FigureWidget
和 make_subplots
可视化来自
Telco Customer Churn dataset(
download 176kB)的多个数据列。
此代码循环遍历 8 列,为每列添加 1 个饼图和 1 个条形图。
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Read data
df = pd.read_csv("./WA_Fn-UseC_-Telco-Customer-Churn.csv")
df['SeniorCitizen'] = df['SeniorCitizen'].map({0: 'No', 1: 'Yes'})
# Define subplot titles and data columns
data_cols = ['PhoneService' ,'MultipleLines' ,'InternetService' ,'OnlineBackup' ,'DeviceProtection' ,'TechSupport' ,'StreamingTV' ,'StreamingMovies']
titles = ['Phone Service' ,'Multiple Lines' ,'Internet Service' ,'Online Backup' ,'Device Protection' ,'Tech Support' ,'Streaming TV' ,'Streaming Movies']
fig = go.FigureWidget(make_subplots(rows=4, cols=4, specs=[
[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}, {'type':'xy'}, {'type':'xy'}],
[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}, {'type':'xy'}, {'type':'xy'}]]))
row, col = 1, 0
for i, (title, data_col) in enumerate(zip(titles, data_cols)):
row, col = divmod(i, 4)
row = row * 2
# Get value counts for pie chart
value_counts = df[data_col].value_counts()
# Create pie chart trace and add to subplot
pie_chart = go.Pie(labels=value_counts.index, values=value_counts.to_numpy(), name=title, title=title)
fig.add_trace(pie_chart, row=row+1, col=col+1)
# get churn rates
churn_counts = df.groupby([data_col, 'Churn'])['Churn'].count().unstack()
# Create stacked bar charts
t1 = go.Bar(name='Churn (yes)', x=churn_counts['Yes'].index, y=churn_counts['Yes'])
t2 = go.Bar(name='Churn (no)', x=churn_counts['No'].index, y=churn_counts['No'], marker_color='indianred')
fig.add_trace(t1, row=row+2, col=col+1)
fig.add_trace(t2, row=row+2, col=col+1)
fig.update_layout(title="Distribution of Customer Services", barmode='stack', showlegend=False)
fig.show()
编辑:将列数减少到两列也无法解决此问题。这是大屏幕上的图表:
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Read data
df = pd.read_csv("./WA_Fn-UseC_-Telco-Customer-Churn.csv")
df['SeniorCitizen'] = df['SeniorCitizen'].map({0: 'No', 1: 'Yes'})
# Define subplot titles and data columns
data_cols = ['PhoneService' ,'MultipleLines' ,'InternetService' ,'OnlineBackup' ,'DeviceProtection' ,'TechSupport' ,'StreamingTV' ,'StreamingMovies']
titles = ['Phone Service' ,'Multiple Lines' ,'Internet Service' ,'Online Backup' ,'Device Protection' ,'Tech Support' ,'Streaming TV' ,'Streaming Movies']
fig = go.FigureWidget(make_subplots(rows=8, cols=2, specs=[
[{'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}],
[{'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}],
[{'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}],
[{'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}]]))
row, col = 1, 0
for i, (title, data_col) in enumerate(zip(titles, data_cols)):
row, col = divmod(i, 2)
row = row * 2
# Get value counts for pie chart
value_counts = df[data_col].value_counts()
# Create pie chart trace and add to subplot
pie_chart = go.Pie(labels=value_counts.index, values=value_counts.to_numpy(), name=title, title=title)
fig.add_trace(pie_chart, row=row+1, col=col+1)
# get churn rates
churn_counts = df.groupby([data_col, 'Churn'])['Churn'].count().unstack()
# Create stacked bar charts
t1 = go.Bar(name='Churn (yes)', x=churn_counts['Yes'].index, y=churn_counts['Yes'])
t2 = go.Bar(name='Churn (no)', x=churn_counts['No'].index, y=churn_counts['No'], marker_color='indianred')
fig.add_trace(t1, row=row+2, col=col+1)
fig.add_trace(t2, row=row+2, col=col+1)
fig.update_layout(title="Distribution of Customer Services", barmode='stack', showlegend=False)
fig.show()
行和列之间的间距可以分别使用
vertical_spacing
和 horizontal_spacing
参数进行调整。两者都可以有 0 到 1/(size -1)
之间的浮点值。需要进行一些修改才能找到适合您的正确值。要最大化图中图表的大小,您可以更新 margin
调用中的 fig.update_layout
参数。所有四个边都在字典中用以下键引用:t
、b
、l
和 r
。
下面的代码是使用所有这些参数对您的代码进行的更改。
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Read data
df = pd.read_csv(".../WA_Fn-UseC_-Telco-Customer-Churn.csv")
df['SeniorCitizen'] = df['SeniorCitizen'].map({0: 'No', 1: 'Yes'})
# Define subplot titles and data columns
data_cols = ['PhoneService' ,'MultipleLines' ,'InternetService' ,'OnlineBackup' ,'DeviceProtection' ,'TechSupport' ,'StreamingTV' ,'StreamingMovies']
titles = ['Phone Service' ,'Multiple Lines' ,'Internet Service' ,'Online Backup' ,'Device Protection' ,'Tech Support' ,'Streaming TV' ,'Streaming Movies']
hor_space = 0.02
ver_space = 0.02
fig = go.FigureWidget(make_subplots(rows=4,
cols=4,
specs=[[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}, {'type':'xy'}, {'type':'xy'}],
[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}],
[{'type':'xy'}, {'type':'xy'}, {'type':'xy'}, {'type':'xy'}]
],
horizontal_spacing=hor_space, # in range 0 to 1/(cols-1)
vertical_spacing=ver_space # in range 0 to 1/(rows-1)
)
)
row, col = 1, 0
for i, (title, data_col) in enumerate(zip(titles, data_cols)):
row, col = divmod(i, 4)
row = row * 2
# Get value counts for pie chart
value_counts = df[data_col].value_counts()
# Create pie chart trace and add to subplot
pie_chart = go.Pie(labels=value_counts.index, values=value_counts.to_numpy(), name=title, title=title)
fig.add_trace(pie_chart, row=row+1, col=col+1)
# get churn rates
churn_counts = df.groupby([data_col, 'Churn'])['Churn'].count().unstack()
# Create stacked bar charts
t1 = go.Bar(name='Churn (yes)', x=churn_counts['Yes'].index, y=churn_counts['Yes'])
t2 = go.Bar(name='Churn (no)', x=churn_counts['No'].index, y=churn_counts['No'], marker_color='indianred')
fig.add_trace(t1, row=row+2, col=col+1)
fig.add_trace(t2, row=row+2, col=col+1)
fig.update_layout(title="Distribution of Customer Services",
barmode='stack',
showlegend=False,
margin={"l":25,
"r":25,
"t":25,
"b":25}
)
fig.show()
plotly 文档提供的一些背景: