How to create a visualization for events along a timeline?

2024/10/15 7:26:22

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.

How to create such a visualization with matplotlib?

This is the visualization being built:

Data visualization of a vehicle

Answer

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

Here is some code to get you started:

import matplotlib.pyplot as plt
import numpy as nprefuel_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()

example plot

https://en.xdnf.cn/q/69314.html

Related Q&A

Multiplying Numpy 3D arrays by 1D arrays

I am trying to multiply a 3D array by a 1D array, such that each 2D array along the 3rd (depth: d) dimension is calculated like:1D_array[d]*2D_arrayAnd I end up with an array that looks like, say:[[ [1…

Django Performing System Checks is running very slow

Out of nowhere Im running into an issue with my Django application where it runs the "Performing System Checks" command very slow. If I start the server with python manage.py runserverIt take…

str.translate vs str.replace - When to use which one?

When and why to use the former instead of the latter and vice versa?It is not entirely clear why some use the former and why some use the latter.

python BeautifulSoup searching a tag

My first post here, Im trying to find all tags in this specific html and i cant get them out, this is the code:from bs4 import BeautifulSoup from urllib import urlopenurl = "http://www.jutarnji.h…

How to remove extra whitespace from image in opencv? [duplicate]

This question already has answers here:How to remove whitespace from an image in OpenCV?(3 answers)Closed 3 years ago.I have the following image which is a receipt image and a lot of white space aroun…

Is there a way in numpy to test whether a matrix is Unitary

I was wondering if there is any function in numpy to determine whether a matrix is Unitary?This is the function I wrote but it is not working. I would be thankful if you guys can find an error in my f…

Two unique marker symbols for one legend

I would like to add a "red filled square" symbol beside the "red filled circle" symbol under legend. How do I achieve this? I prefer to stick with pyplot rather than pylab. Below i…

What is Rubys equivalent to Pythons multiprocessing module?

To get real concurrency in Ruby or Python, I need to create new processes. Python makes this pretty straightforward using the multiprocessing module, which abstracts away all the fork / wait goodness a…

Using grep in python

There is a file (query.txt) which has some keywords/phrases which are to be matched with other files using grep. The last three lines of the following code are working perfectly but when the same comma…

shuffling a list with restrictions in Python

I have a problem with randomizing a list with restrictions in Python (3). I have seen a few other questions relating to this, but none of them really seem to solve my problem. Im a beginner, so any hel…