2017-08-14 117 views
3

我有一個我在Jupyter筆記本中創建的報告。我希望輸出(繪圖)以美觀爲中心。如何將輸出集中在Python Jupyter筆記本上?

我試圖在這裏給出了答案:

Centering output on IPython notebook

然而,這一個不工作。

我發現這裏面的工程#1(Center align outputs in ipython notebook

CSS = """ 
.output { 
    align-items: center; 
} 
""" 

HTML('<style>{}</style>'.format(CSS)) 

然而,儘管它的中心情節,當劇情寬,也不需要爲中心,它擴展了它,並使它比我不想要的頁面更寬。我試圖調節輸出邊緣區域像它說,但無論是它再次推到左側或者squishes到它需要滾動條的點(再次我不希望出現這種情況)

enter image description here

任何人有有什麼建議麼?我認爲這將是標準和容易的,但顯然不是(如果我想要的是不可能的話,僅僅居中一個代碼塊的方法將是一個完美的解決方法?)

即,中心這個表:

enter image description here

它是此代碼生成:

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') 
+0

你只是想中心在筆記本的現場版的輸出,或做你想讓它後中心'nbconvert'將它轉換爲例如HTML,PDF?或者你想讓它在兩種場景中居中? –

+0

嘿,謝謝你的迴應!當你通過筆記本將它輸出到html時,我希望它將輸出居中) – ScoutEU

回答

1

請試訓的這個類居中你的圖表,因爲沒有數據框中提供的,我創建了隨機的數據幀展示的能力曲線類。請檢查一下。

代碼:

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) 

輸出:

enter image description here

+0

謝謝Naren,現在就去看看:) – ScoutEU

+0

嘿,首先感謝你的幫助,你的代碼的確將輸出集中在一起,但像我之前的問題一樣,當你將它導出爲HTML時,它會因爲某種原因擠壓圖表並添加一個水平滾動條:/ ...我真的不知道爲什麼。 – ScoutEU

+0

@ScoutEU讓我出口並檢查 –