如何为时间轴上的事件创建可视化? [英] How to create a visualization for events along a timeline?

查看:334
本文介绍了如何为时间轴上的事件创建可视化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python构建可视化. 我想在那里可视化加油站和汽车的加油成本.此外,洗车及其费用以及修理应可视化.燃料成本和洗衣成本应根据成本而有较高的标准.我在下面创建了可视化来描述这些概念.

I'm building a visualization with Python. There I'd like to visualize fuel stops and the fuel costs of my car. Furthermore, car washes and their costs should be visualized as well as repairs. The fuel costs and laundry costs should have a higher bar depending on the costs. I created the visualization below to describe the concepts.

如何使用matplotlib创建这种可视化效果?

How to create such a visualization with matplotlib?

这是正在构建的可视化:

This is the visualization being built:

推荐答案

是的,使用matplotlib完全可以实现这种可视化.为了存储数据,通常使用numpy数组.

Yes, this kind of visualization is perfectly possible with matplotlib. To store the data, numpy arrays are usually very handy.

以下是一些入门代码:

import matplotlib.pyplot as plt
import numpy as np

refuel_km = np.array([0, 505.4, 1070, 1690])
refuel_cost = np.array([40.1, 50, 63, 55])

carwash_km = np.array([302.0, 605.4, 901, 1331, 1788.2])
carwash_cost = np.array([35.0, 40.0, 35.0, 35.0, 35.0])

repair_km = np.array([788.0, 1605.4])
repair_cost = np.array([135.0, 74.5])

fig, ax = plt.subplots(figsize=(12,3))

plt.scatter(refuel_km, np.full_like(refuel_km, 0), marker='o', s=100, color='lime', edgecolors='black', zorder=3, label='refuel')
plt.bar(refuel_km, refuel_cost, bottom=15, color='lime', ec='black', width=20, label='refuel cost')

plt.scatter(carwash_km, np.full_like(carwash_km, 0), marker='d', s=100, color='tomato', edgecolors='black', zorder=3, label='car wash')
plt.bar(carwash_km, -carwash_cost, bottom=-15, color='tomato', ec='black', width=20, label='car wash cost')

plt.scatter(repair_km, np.full_like(repair_km, 0), marker='^', s=100, color='lightblue', edgecolors='black', zorder=3, label='car repair')
#plt.bar(repair_km, -repair_cost, bottom=-15, color='lightblue', ec='black', width=20)

ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
ax.tick_params(axis='x', length=20)
ax.set_yticks([])  # turn off the yticks

_, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
ax.set_xlim(-15, xmax)
ax.set_ylim(ymin, ymax+25) # make room for the legend
ax.text(xmax, -5, "km", ha='right', va='top', size=14)
plt.legend(ncol=5, loc='upper left')

plt.tight_layout()
plt.show()

这篇关于如何为时间轴上的事件创建可视化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆