In Python, an area chart that has been split up into slices and the slices then layered on top of one another with the areas representing the highest values on top is known as Horizon Chart.
The main purpose of the horizon chart is that, it separates the surface of a celestial body from its sky when viewed from the perspective of an observer on or near the surface of the relevant body and this curve divides all viewing directions based on whether it intersects the relevant body's surface or not. Advantage: simplicity, security, speed and scale.
- Matplotlib: a library which is used for creating static, animated, and interactive visualizations in Python, making easy and hard things possible and it creates publication quality plots and it also makes interactive figures that can update, zoom, and pan.
- NumPy: this library is used to work with Python arrays and has functions to work in domain of fourier transform, linear algebra and matrices.
Here is an example of how to create a horizon chart using Matplotlib in Python:
Fig: Preview of the output that you will get on running this code from your IDE.
In this solution we're using Matplotlib and NumPy libraries.
import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.cbook as cbook years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month years_fmt = mdates.DateFormatter('%Y') # Load a numpy structured array from yahoo csv data with fields date, open, # close, volume, adj_close from the mpl-data/example directory. This array # stores the date as an np.datetime64 with a day unit ('D') in the 'date' # column. with cbook.get_sample_data('goog.npz') as datafile: data = np.load(datafile)['price_data'] fig, ax = plt.subplots() ax.plot('date', 'adj_close', data=data) # format the ticks ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(years_fmt) ax.xaxis.set_minor_locator(months) # round to nearest years. datemin = np.datetime64(data['date'], 'Y') datemax = np.datetime64(data['date'][-1], 'Y') + np.timedelta64(1, 'Y') ax.set_xlim(datemin, datemax) # format the coords message box ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax.format_ydata = lambda x: '$%1.2f' % x # format the price. ax.grid(True) # rotates and right aligns the x labels, and moves the bottom of the # axes up to make room for them fig.autofmt_xdate() plt.show()
Follow the steps carefully to get the output easily.
- Install PyCharm Community Edition on your computer.
- Open terminal and install the required libraries with following commands.
- Install Matplotlib - pip install matplotlib.
- Install NumPy - pip install numpy.
- Create a new Python file(eg: test.py).
- Copy the snippet using the 'copy' button and paste it into that file.
- Run the file using run button.
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
I found this code snippet by searching for 'how to make horizontal axis chart show date' in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions.
- The solution is created in PyCharm 2022.3.3.
- The solution is tested on Python 3.9.7.
- Matplotlib version 3.7.1.
- NumPy version v1.24.2.
Using this solution, we are able to create horizon chart using Matplotlib in Python with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to create horizon chart using Matplotlib in Python.