简体   繁体   English

在甘特图上显示问题(Jupyter,Plotly,Python)

[英]Display issue on Gantt Chart (Jupyter,Plotly,Python)

I'm trying the plotly library and I made a Gantt chart in jupyter with a fake list of students and when they enter/exit a language school. 我正在尝试图书馆,我在jupyter中制作了一张甘特图,上面有假学生名单,以及他们进入/退出语言学校的时间。

The create_gantt method interpreted the data correctly but the display is somehow cropped on the left. create_gantt方法可以正确解释数据,但是显示会在左侧裁剪。 Only 11 characters or so from the full names are displayed. 全名中仅显示11个字符左右。

If you look closely on the page https://plot.ly/python/gantt/ in "Out[7]:" the M of the task "Morning Sleep" is also cropped. 如果您在“ Out [7]:”中的页面https://plot.ly/python/gantt/上仔细查看,也会裁剪“早晨睡眠”任务的M。

I found no arguments in the method to change this. 我没有在方法中更改此参数。 I also tried modifiying jupyter's display properties and it didn't affect the chart: 我还尝试修改jupyter的显示属性,但它不影响图表:

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

Does someone have an idea to fix this? 有人有解决此问题的想法吗? Thank you very much :). 非常感谢你 :)。

Here's the 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')

甘特图

so I figured out there is a forum for plotly and someone answered there ( https://community.plot.ly/t/gantt-cropped-text/7053/3 ). 所以我发现那里有一个供密谋的论坛,有人在那儿回答( https://community.plot.ly/t/gantt-cropped-text/7053/3 )。

I managed to solve the problem programmatically. 我设法以编程方式解决了这个问题。 I calculate the max width of the task column in pixel using a function. 我使用函数计算任务列的最大宽度(以像素为单位)。 After creating the chart I use an uptade method and give the max width as an argument. 创建图表后,我使用uptade方法并将最大宽度作为参数。 Then I display the chart. 然后,我显示图表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM