python使用pyecharts、pandas库从E:\数据可视化\数据可视化第三次作业\1.xlsx导入数据,绘制横轴为开始时间和结束时间,数轴为任务名称,数值为交货时间的甘特图
时间: 2024-02-24 17:59:08 浏览: 28
好的,你可以按照以下步骤操作:
1. 首先,安装pyecharts和pandas库,可以使用以下命令:
```
pip install pyecharts
pip install pandas
```
2. 然后,导入需要的库:
```python
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.globals import ThemeType
```
3. 接下来,读取Excel文件中的数据:
```python
df = pd.read_excel(r'E:\数据可视化\数据可视化第三次作业\1.xlsx')
```
4. 根据数据绘制甘特图:
```python
# 获取任务名称、开始时间、结束时间和交货时间数据
task_names = df['任务名称'].tolist()
start_times = df['开始时间'].tolist()
end_times = df['结束时间'].tolist()
delivery_times = df['交货时间'].tolist()
# 计算任务的持续时间
durations = [(end - start).days + 1 for start, end in zip(start_times, end_times)]
# 创建一个时间轴对象
timeline = Timeline(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
# 循环遍历每个任务,创建一个甘特图
for i in range(len(task_names)):
# 创建一个柱状图对象
bar = (
Bar()
.add_xaxis(['任务时间'])
.add_yaxis(task_names[i], [(start_times[i], end_times[i])])
.set_global_opts(title_opts=opts.TitleOpts(title='甘特图'))
.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_='max', name='最长时间')]
),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='average', name='平均时间'),
opts.MarkLineItem(
type_='max', name='最长时间', symbol='arrow', symbol_size=[8, 16]
),
]
),
)
)
# 添加交货时间
bar.add_js_funcs(
f"chart.setOption({{'timeline':{{currentIndex:{i}}}}});"
f"chart.setOption({{'series':[{{'data':["
f"{{'value': {delivery_times[i]}, 'itemStyle': {{'color': 'red'}}}}]"
f"}}]}});"
)
# 添加到时间轴中
timeline.add(bar, f'{task_names[i]}({durations[i]}天)')
# 渲染并保存甘特图
timeline.render('甘特图.html')
```
这段代码会生成一个时间轴对象,循环遍历每个任务,创建一个柱状图对象,设置其样式和交货时间,并添加到时间轴中。最后,渲染并保存甘特图。你可以根据自己的需要修改代码,比如更改Excel文件路径、修改样式等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)