在Python中,你可以使用`matplotlib`或者`plotly`库来制作甘特图¹²⁴⁵。以下是一些示例:


**使用matplotlib制作甘特图**¹:

```python

import matplotlib.pyplot as plt


# 设置字体和负号显示

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False


# 绘制条形图

plt.barh(y=1, width=2, left=0, edgecolor='white', color='#8CD77F')

plt.barh(y=2, width=2, left=1, edgecolor='white', color='#B1DFE7')

plt.barh(y=3, width=1, left=2, edgecolor='white', color='#B9E9C8')

plt.barh(y=4, width=3, left=3, edgecolor='white', color='#AEC4E5')

plt.barh(y=5, width=3, left=2, edgecolor='white', color='#D28670')


# 设置标题和坐标轴标签

plt.title("甘特图")

plt.yticks([1, 2, 3, 4, 5], ['项目1', '项目2', '项目3', '项目4', '项目5'])

plt.xticks([0, 1, 2, 3, 4, 5, 6], ['4月', '5月', '6月', '7月', '8月', '9月', '10月'])


# 显示图形

plt.show()

```


**使用plotly制作甘特图**⁴⁵:

```python

import plotly.figure_factory as ff


# 创建数据

df = [dict(Task="任务 A", Start='2023-01-01', Finish='2023-02-02', Resource="资源 1"),

     dict(Task="任务 B", Start='2023-03-05', Finish='2023-04-15', Resource="资源 2"),

     dict(Task="任务 C", Start='2023-02-20', Finish='2023-05-30', Resource="资源 3")]


# 创建甘特图

fig = ff.create_gantt(df, index_col='Resource', show_colorbar=True, group_tasks=True)


# 显示图形

fig.show()

```


这些只是基本的示例,实际的代码可能会根据你的需求有所不同。在编写代码时,你可能需要参考`matplotlib`或`plotly`库的文档¹²⁴⁵。希望这个信息对你有所帮助!如果你有其他问题,欢迎随时向我提问。¹²⁴⁵。

在Python中,你可以使用面向对象的方法来创建一个甘特图的模板。以下是一个使用`matplotlib`库的例子¹:


```python

import matplotlib.pyplot as plt


class GanttChart:

   def __init__(self, tasks, start_dates, end_dates):

       self.tasks = tasks

       self.start_dates = start_dates

       self.end_dates = end_dates


   def draw_chart(self):

       plt.figure(figsize=(10, 6))


       for i in range(len(self.tasks)):

           plt.barh(self.tasks[i], left=self.start_dates[i], width=self.end_dates[i]-self.start_dates[i], color='blue')


       plt.xlabel('Time')

       plt.ylabel('Task')

       plt.grid(True)

       plt.show()


# 使用示例

tasks = ['Task 1', 'Task 2', 'Task 3']

start_dates = [1, 3, 2]

end_dates = [2, 5, 4]


chart = GanttChart(tasks, start_dates, end_dates)

chart.draw_chart()

```


在这个例子中,我们首先定义了一个名为`GanttChart`的类,它有一个构造函数和一个`draw_chart`方法。构造函数接受任务列表、开始日期列表和结束日期列表作为参数,并将它们存储为类的属性。`draw_chart`方法使用`matplotlib`的`barh`函数来绘制甘特图¹。


然后,我们创建了一个`GanttChart`的实例,并调用`draw_chart`方法来绘制甘特图¹。


这只是一个基本的例子,实际的代码可能会根据你的需求有所不同。在编写代码时,你可能需要参考`matplotlib`库的文档¹。希望这个信息对你有所帮助!如果你有其他问题,欢迎随时向我提问。¹。


源: 与必应的对话, 2023/12/18

(1) 可视化神器Plotly玩转甘特图 - 知乎. https://zhuanlan.zhihu.com/p/369128402.

(2) Python实现生产过程可视化(甘特图) - 知乎. https://zhuanlan.zhihu.com/p/106787569.

(3) Python学习:如何绘制甘特图 Gantt ? - 知乎. https://zhuanlan.zhihu.com/p/536274616.

(4) ChatGPT教你如何用Python和Matplotlib绘图(上) - 知乎. https://zhuanlan.zhihu.com/p/615381673.

(5) 10分钟用Python或MATLAB制作漂亮的甘特图(Gantt) - 阿里云开发者社区. https://developer.aliyun.com/article/893170.

(6) undefined. https://github.com/PyGantt/PyGantt有以下四个csv.

源: 与必应的对话, 2023/12/18

(1) Python利用matplotlib绘制甘特图的三种方式 - CSDN博客. https://blog.csdn.net/hfy1237/article/details/121454511.

(2) Python实现生产过程可视化(甘特图) - 知乎. https://zhuanlan.zhihu.com/p/106787569.

(3) 【Python】Plotly 绘制甘特图_python绘制甘特图-CSDN博客. https://blog.csdn.net/nixiang_888/article/details/115612723.

(4) Python 绘制甘特图 - 努力的孔子 - 博客园. https://www.cnblogs.com/yanshw/p/14743702.html.

(5) python绘制甘特图代码_python实时甘特图画法代码-CSDN博客. https://bing.com/search?q=python%e5%88%b6%e4%bd%9c%e7%94%98%e7%89%b9%e5%9b%be+%e4%bb%a3%e7%a0%81.