2017-11-27 3 views
1

Ich versuche die Plotly-Bibliothek und ich machte ein Gantt-Diagramm in Jupiter mit einer falschen Liste von Studenten und wenn sie eine Sprachschule betreten/verlassen.Anzeige Problem auf Gantt-Diagramm (Jupyter, Plotly, Python)

Die Methode create_gantt hat die Daten korrekt interpretiert, aber die Anzeige ist auf der linken Seite abgeschnitten. Von den vollständigen Namen werden nur 11 Zeichen angezeigt.

Wenn Sie genau auf der Seite https://plot.ly/python/gantt/ in "Out [7]:" schauen, wird das M der Aufgabe "Morning Sleep" ebenfalls beschnitten.

Ich fand keine Argumente in der Methode, dies zu ändern. Ich habe auch versucht jupyter die Anzeigeeigenschaften modifiying und es hat keinen Einfluss auf das Diagramm:

from IPython.core.display import display, HTML 
display(HTML("<style>.container { width:100%; }</style>")) 

Hat jemand eine Idee hat dieses Problem beheben? Vielen Dank :).

Hier ist der Code:

import pandas as pd 
from plotly.offline import init_notebook_mode, iplot 
from plotly.graph_objs import * 
init_notebook_mode(connected=True) # initiate notebook for offline plot 
import plotly.figure_factory as ff 

df_gantt2 =pd.DataFrame([ 
{'Task': 'Anthony Clark', 'Start': '2017-12-13', 'Finish': '2018-02-23'}, 
{'Task': 'Ariosto Li Fonti', 'Start': '2017-12-15', 'Finish': '2018-01-23'}, 
{'Task': 'Cettina Trevisano', 'Start': '2017-12-20', 'Finish': '2018-03-08'}, 
{'Task': 'Dora Padovesi', 'Start': '2018-01-11', 'Finish': '2018-01-12'}, 
{'Task': 'Emmeline Déziel', 'Start': '2018-01-22', 'Finish': '2018-03-25'}, 
{'Task': 'Sawa Tretyakov', 'Start': '2018-12-03', 'Finish': '2018-12-31'},]) 

fig = ff.create_gantt(df_gantt2, colors=['#333F44', '#93e4c1'],title='Students\' presence (those are fake names)',show_colorbar=True, bar_width=0.2, showgrid_x=True, showgrid_y=True) 
iplot(fig,filename = 'students-presence-gantt') 

Gantt Chart Plotly

Antwort

2

so dachte ich, da draußen ist ein Forum für plotly und jemand antwortete dort (https://community.plot.ly/t/gantt-cropped-text/7053/3).

Ich habe es geschafft, das Problem programmgesteuert zu lösen. Ich berechne die maximale Breite der Aufgabenspalte in Pixel unter Verwendung einer Funktion. Nach dem Erstellen des Diagramms verwende ich eine uptade-Methode und gebe die maximale Breite als Argument an. Dann zeige ich das Diagramm an.

def max_length_col(column,font='OpenSans-Regular.ttf', font_size=14): 
'''Calculates the max length of a column of a dataframe/a panda serie in pixels. 
Default keyword arguments values are useful to adapt the length of the y axis of a plotly gantt chart. 

Args: 
    column: panda serie 
    font: ttf filename (look under ...\Windows\Font, get the exact name by right-clicking on a ttf file and then go to properties) 
    font_size : font size as an int 

Example: 
    In: 
     df_gantt =pd.DataFrame([ 
     {'Task': 'Anthony Clark', 'Start': '2017-12-13', 'Finish': '2018-02-23'}, 
     {'Task': 'Ariosto Li Fonti', 'Start': '2017-12-15', 'Finish': '2018-01-23'}, 
     {'Task': 'Cettina Trevisano', 'Start': '2017-12-20', 'Finish': '2018-03-08'}]) 
     column_len = max_length_col(df_gantt['Task']) 
     print(column_len) 
    Out: 
     117 

Returns: 
    Length of the column in pixel as an int 
''' 
from PIL import ImageFont #pip install pillow 
font = ImageFont.truetype(font,font_size) # should already be installed, if not download it and save under Windows/Font 
length_list = [] 
for row in range(len(column)): 
    text = str(column[row]) 
    size = font.getsize(text) 
    length_list.append(size[0]) # append length in pixel (size[1] for heigth) 
max_length_px = max(length_list) 
return max_length_px 

import pandas as pd 
from plotly.offline import init_notebook_mode, iplot 
from plotly.graph_objs import * 
init_notebook_mode(connected=True) # initiate notebook for offline plot 
import plotly.figure_factory as ff 

df_gantt2 =pd.DataFrame([ 
{'Task': 'Anthony Clark', 'Start': '2017-12-13', 'Finish': '2018-02-23'}, 
{'Task': 'Ariosto Li Fonti', 'Start': '2017-12-15', 'Finish': '2018-01-23'}, 
{'Task': 'Cettina Trevisano', 'Start': '2017-12-20', 'Finish': '2018-03-08'}, 
{'Task': 'Dora Padovesi', 'Start': '2018-01-11', 'Finish': '2018-01-12'}, 
{'Task': 'Emmeline Déziel', 'Start': '2018-01-22', 'Finish': '2018-03-25'}, 
{'Task': 'Sawa Tretyakov', 'Start': '2018-12-03', 'Finish': '2018-12-31'},]) 

fig = ff.create_gantt(df_gantt2, colors=['#333F44', '#93e4c1'],title='Students\' presence (those are fake names)',show_colorbar=True, bar_width=0.2, showgrid_x=True, showgrid_y=True) 

column_len = df_gantt2['Task'] 
max_length_px = max_length_col(column_len) 

fig['layout'].update(autosize=False, width=800, height=500, margin=dict(l=max_length_px)) 
#Insert this line just after fig=ff.create_gantt 
iplot(fig,filename = 'students-presence-gantt')