Джейд Картер – Библиотеки Python Часть 2. Практическое применение (страница 25)
for i in range(1, len(days) + 1)
]
fig.update(frames=frames)
# Настройка кнопок
fig.update_layout(
updatemenus=[
dict(
type='buttons',
showactive=False,
buttons=[
dict(label='Play', method='animate', args=[None, {'frame': {'duration': 500, 'redraw': True}}]),
dict(label='Pause', method='animate', args=[[None], {'frame': {'duration': 0, 'redraw': False}}])
]
)
]
)
# Оформление графика
fig.update_layout(
title='Изменение температуры по дням недели',
xaxis_title='День недели',
yaxis_title='Температура (°C)',
template='plotly_white'
)
fig.show()
```
Задача 7: Трёхмерная анимация COVID-19
Описание:
Используйте вымышленные данные о росте случаев COVID-19 в трёх странах (`USA`, `India`, `Brazil`) за шесть месяцев:
– Месяцы: `['January', 'February', 'March', 'April', 'May', 'June']`
– Число случаев (матрица):
```
USA: [1000, 2000, 4000, 8000, 15000, 20000]
India: [500, 1500, 3000, 6000, 12000, 18000]
Brazil: [800, 1600, 3200, 6400, 13000, 19000]
```
Создайте трёхмерную анимацию, показывающую рост числа случаев по месяцам.
Решение:
```python
import plotly.graph_objects as go
# Данные
months = ['January', 'February', 'March', 'April', 'May', 'June']
countries = ['USA', 'India', 'Brazil']
cases = {
'USA': [1000, 2000, 4000, 8000, 15000, 20000],
'India': [500, 1500, 3000, 6000, 12000, 18000],
'Brazil': [800, 1600, 3200, 6400, 13000, 19000]
}
# Построение графика
fig = go.Figure()
for month, idx in zip(months, range(len(months))):
fig.add_trace(go.Scatter3d(
x=countries,
y=[month] * len(countries),
z=[cases[country][idx] for country in countries],
mode='markers',
marker=dict(size=10, color=[cases[country][idx] for country in countries], colorscale='Viridis'),
name=month
))
# Оформление графика
fig.update_layout(
title='Трёхмерная анимация роста COVID-19',
scene=dict(
xaxis_title='Страна',
yaxis_title='Месяц',
zaxis_title='Число случаев'