我正在尝试这个阴谋图书馆,我在jupyter中制作了一张甘特图,上面有一个假的学生名单,当他们进入/退出一所语言学校时。
create_gantt方法正确解释了数据,但显示在左侧以某种方式裁剪。仅显示全名中的11个字符。
如果仔细查看“Out [7]:”页面https://plot.ly/python/gantt/,“早晨睡眠”任务的M也会被裁剪。
我发现方法中没有任何参数可以改变它。我也尝试修改jupyter的显示属性,它不会影响图表:
{{1}}
有人有想法解决这个问题吗?非常感谢你:)。
以下是代码:
{{1}}
答案 0 :(得分:2)
所以我发现有一个剧情的论坛,有人在那里回答(https://community.plot.ly/t/gantt-cropped-text/7053/3)。
我设法以编程方式解决问题。我使用函数计算任务列的最大宽度(以像素为单位)。创建图表后,我使用uptade方法并将最大宽度作为参数。然后我显示图表。
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')