[英]How do I center the outputs on a Python Jupyter notebook?
我有一個在 Jupyter Notebook 中創建的報告。 出於美學原因,我希望輸出(繪圖)居中。
我已經嘗試過這里給出的答案:
然而,這個不起作用。
我確實發現這個適用於 Stackoverflow( 在 ipython notebook 中居中對齊輸出)
CSS = """
.output {
align-items: center;
}
"""
HTML('<style>{}</style>'.format(CSS))
然而,雖然它使繪圖居中,但當繪圖很寬並且不需要居中時,它會擴展它並使它比我不想要的頁面更寬。 我試過像它所說的那樣調整輸出邊距區域,但它要么再次將其推向左側,要么將其壓扁到需要滾動條的程度(同樣我不想要那樣)
有人有什么建議嗎? 我認為這將是標准和簡單的,但顯然不是(如果我想要的是不可能的,那么僅將代碼塊居中的方法將是一種完美的解決方法?)
即以這張表為中心:
這是由這段代碼產生的:
df = pd.DataFrame(a01)
new_df01 = df[['Call','FirstReceivedDate','Value']]
new_df01['month'] = pd.Categorical(new_df01['FirstReceivedDate'].dt.strftime('%b'),
categories=vals, ordered=True)
groupA01 = new_df01.groupby(['Call']).agg({'Value':sum, 'FirstReceivedDate':'count'}).rename(columns={'FirstReceivedDate':'Count'})
groupA01['Value'] = groupA01['Value'].map('{:,.2f}'.format)
def hover(hover_color="#F1C40F"):
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)])
styles2 = [
hover(),
dict(selector="th", props=[("font-size", "80%"),
("font-family", "Gill Sans MT"),
("color",'white'),
('background-color', 'rgb(11, 48, 79)'),
("text-align", "center")]),
dict(selector="td", props=[("font-size", "75%"),
("font-family", "Gill Sans MT"),
("text-align", "center")]),
dict(selector="tr", props=[("line-height", "11px")]),
dict(selector="caption", props=[("caption-side", "bottom")])
]
html2 = (groupA01.style.set_table_styles(styles2)
.set_caption(""))
html2
謝謝!
添加代碼以顯示熱圖的繪制:
dfreverse = df_hml.values.tolist()
dfreverse.reverse()
colorscale = [[0,'#FFFFFF'],[0.5, '#454D59'], [1, '#F1C40F']]
x = [threeYr,twoYr,oneYr,Yr]
y = ['March', 'February', 'January', 'December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April']
z = dfreverse
hovertext = list()
for yi, yy in enumerate(y):
hovertext.append(list())
for xi, xx in enumerate(x):
hovertext[-1].append('Count: {}<br />{}<br />{}'.format(z[yi][xi],yy, xx))
data = [plotly.graph_objs.Heatmap(z=z,
colorscale=colorscale,
x=x,
y=y,
hoverinfo='text',
text=hovertext)]
layout = go.Layout(
autosize=False,
font=Font(
family="Gill Sans MT",
size = 11
),
width=600,
height=450,
margin=go.Margin(
l=0,
r=160,
b=50,
t=100,
pad=3
),
xaxis=dict(
title='',
showgrid=False,
titlefont=dict(
# family='Gill sans, monospace',
size=12,
#color='#7f7f7f'
),
showticklabels=True,
tickangle=25,
tickfont=dict(
family="Gill Sans MT",
size=12,
color='black'
),
),
yaxis=dict(
title='',
showgrid=False,
titlefont=dict(
#family='Gill sans',
#size=12,
#color='#7f7f7f'
),
showticklabels=True,
tickangle=25,
tickfont=dict(
family="Gill Sans MT",
size=12,
color='black'
),
)
)
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig,config={"displayModeBar": False},show_link=False,filename='pandas-heatmap')
請試用這個類來使您的圖形居中,因為沒有提供數據框,我正在創建一個帶有隨機數據框的圖來展示該類的功能。 請檢查一下。
代碼:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from IPython.display import display, HTML
from plotly.graph_objs import *
import numpy as np
init_notebook_mode(connected=True)
display(HTML("""
<style>
.output {
display: flex;
align-items: center;
text-align: center;
}
</style>
"""))
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
iplot([Box(y = np.random.randn(50), showlegend=False) for i in range(45)], show_link=False)
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.