Techno Blender
Digitally Yours.

3 Unique Charts You Wouldn’t Think Were Created with Matplotlib | by Andy McDonald | Mar, 2023

0 69


Utilising Python’s Matplotlib to Create Advanced Data Visualisations

Example Gantt chart created using Python’s matplotlib data visualisation library. Image by Andy McDonald
Example Gantt chart created using matplotlib — image by the author.

Often matplotlib has a bit of a reputation for creating boring figures and being awkward to use. However, with some patience, research and extra lines of python code, we can create some unique and very visually appealing figures.

When learning python, matplotlib is often the first data visualisation library that people come across. With a few lines of code, you may end up generating a figure like the one below. Shortly afterwards, many people then move on to learning Seaborn or Plotly and they never look back.

Example of a basic matplotlib scatterplot. Image by the author.

However, there may be times when you want to create a very specific figure, and these high-level libraries are not able to do precisely what you want. Or maybe, you want to have some fun creating unique visualisations. This is where matplotlib comes back into play.

Matplotlib is a low-level library that allows you to modify nearly all of the elements within the figure. This ranges from labels to tick marks, axes, and much more. However, it can be tiresome and awkward to work with, and often you have to resort to using Google and StackOverflow to understand how to modify certain elements.

I don’t hate using some of the higher-level plotting libraries; however, I often find myself limited by some of their functionality, especially when it comes to working with well-log data, and creating figures for publications.

Within this article, we will take a quick look at three unique visualisations that can be generated with matplotlib that you may not have thought possible.

Gantt charts are a popular visualisation tool for tracking the progress of projects. They were first popularised by Henry Gantt in the 1910s, although the precursor to these charts can be traced back to 1896 when they were referred to harmanograms by Karol Adamiecki.

Gantt charts are commonly used in project management and can provide a wealth of information about a project, including:

  • provides an overview of how a project is progressing
  • what tasks are complete
  • what tasks are still to be done
  • how long are tasks supposed to take
  • and much more

Even though a wealth of tools (paid and free) are available for project management, there may be an occasion when you want to create a Gantt Chart directly in Python—for example, displaying a Gantt chart on a Streamlit dashboard.

The example below utilises matplotlib’s horizontal barplot to display each task. Utilising the left parameter, we can start the parts at the expected start dates and make them the full length of the task duration.

We have also coloured each task by its project phase, which could easily be swapped out for task categories or to indicate people assigned to that task.

The red line on the plot indicates the current date and will change position if the code has been rerun at a later date than the previous run.

Example Gantt chart created using Python’s matplotlib data visualisation library. Image by Andy McDonald
Example Gantt chart created using matplotlib—image by the author.

If you want to recreate the above Gantt chart, you can do so with the following code:

import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import datestr2num, DateFormatter, DayLocator
from matplotlib.ticker import AutoMinorLocator
from matplotlib.patches import Patch

# Create dummy date
tasks = ['Task A', 'Task B', 'Task C', 'Task D', 'Task E', 'Task F', 'Task G', 'Task H', 'Task I', 'Task J']
start_dates = ['2023-02-25', '2023-03-10', '2023-03-13', '2023-03-23', '2023-04-01', '2023-04-05', '2023-04-12', '2023-04-20', '2023-04-24', '2023-05-02']
end_dates = ['2023-03-03', '2023-03-17', '2023-03-22', '2023-03-30', '2023-04-07', '2023-04-18', '2023-04-23', '2023-04-25', '2023-05-03', '2023-05-07']

# Setup the dates and calculate durations
start_dates = [datestr2num(d) for d in start_dates]
end_dates = [datestr2num(d) for d in end_dates]

durations = [(end - start) for start, end in zip(start_dates, end_dates)]

fig, ax = plt.subplots(figsize=(15, 8), facecolor='#25253c')

ax.set_facecolor('#25253c')

# Create colours for each task based on categories
colors = ['#7a5195', '#ef5675', '#ffa600']
task_colors = [colors[0]] * 3 + [colors[1]] * 4 + [colors[2]] * 3

# Display the bars
ax.barh(y=tasks, width=durations, left=start_dates,
height=0.8, color=task_colors)

ax.invert_yaxis()

# Setup the x axis labels
ax.set_xlim(start_dates[0], end_dates[-1])

date_form = DateFormatter("%Y-%m-%d")
ax.xaxis.set_major_formatter(date_form)

ax.xaxis.set_major_locator(DayLocator(interval=10))
ax.xaxis.set_minor_locator(AutoMinorLocator(5))
ax.tick_params(axis='x', which='minor', length=2, color='white', labelsize=6)

ax.get_yaxis().set_visible(False)

# Control the colour of the grid for major and minor lines
ax.grid(True, axis='x', linestyle='-', color='#FFFFFF', alpha=0.2, which='major')
ax.grid(True, axis='x', linestyle='-', color='#FFFFFF', alpha=0.05, which='minor')
ax.set_axisbelow(True)

# Add labels for each task. For padding, we can use an f-string and add some extra space
for i, task in enumerate(tasks):
ax.text(start_dates[i], i, f' {task}', ha='left', va='center', color='white', fontsize=12, fontweight='bold')

# Add the current date line
today = datetime.datetime.now().strftime("%Y-%m-%d")
today_num = datestr2num(today)
ax.axvline(today_num, color='red', alpha=0.8)

# Style ticks, labels and colours
ax.tick_params(axis='both', colors='white')

ax.set_xlabel('Date', color='white', fontsize=12)
ax.set_title('Project Schedule', color='white', fontsize=14)

# Hide spines so only bottom is visible
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Create a list of custom patches for the legend
legend_elements = [
Patch(facecolor=colors[0], label='Planning'),
Patch(facecolor=colors[1], label='Development'),
Patch(facecolor=colors[2], label='Testing'),
]

# Add the legend in the top right corner of the plot
ax.legend(handles=legend_elements, loc='upper right',
facecolor='white',
edgecolor='white',
fontsize=10, title='Phases', title_fontsize=12, frameon=True)

plt.show()

Example of a matplotlib donut chart with a figure in the centre. Image by the author.

Donut charts are a simple alternative to the common pie chart. Instead of categories being represented as slices in a pie chart, they are instead represented as arcs on the donut chart. This allows the reader to focus on the length of the arc rather than angles and areas.

Commonly the centre of the donut chart is left blank, but it provides an excellent opportunity to place key information or data. For example, within financial dashboards, we may have a revenue target to reach, representing the whole donut. The portion of the donut that is filled can be used to represent revenue earned, and the centre of the chart can be used to provide the total figure for that earned revenue.

Alternatively, they could be used to monitor progress, with the completed progress represented as a percentage in the centre.

The above donut chart can be created with the following code.

Adding the figures to the centre of the donut is achieved by using matplotlibs text function. This allows us to control the format and position of the text.

Radial bar chart as an alternative to the conventional bar chart. Image by the author.

Often on dashboards or infographics, we have radial bar charts presented. These are visually attractive alternatives to traditional bar charts. Instead of plotting data using the conventional cartesian coordinate system, the bars are represented as rings using the polar coordinate system.

Radial bar charts are visually appealing graphics to include in presentations or posters. However, they do have a few drawbacks. They can be difficult to understand due to our visual system finding ittougher to interpret curved lines compared to straight lines. Additionally, the centre radial bars can be harder to read compared to those further out.

The above radial bar chart can be created using the code below.

From the code, we can see that we have to create two axes, one that uses regular cartesian coordinates (for the background figure) and one that uses polar coordinates.

We can then loop through each category in the dataset and add them as a ring on the chart.

Additionally, choosing appropriate colours for visualisations can be subjective and can be a time-consuming process. If you are looking for resources on how to make the colour selection easier, then I recommend checking out my article on 4 Essential Tools to Help You Select a Colour Palette for Your Data Visualisation.

If you want to understand more about how this figure was constructed, then I check out my article below:

As you can see, matplotlib is not limited to creating simple figures with blue points or lines and no labelling. With a little bit of effort, you can customise the plots to suit your style and needs. Even though there are many other libraries available for creating data visualisations with fewer lines of code, I always find myself coming back to matplotlib to create figures.


Utilising Python’s Matplotlib to Create Advanced Data Visualisations

Example Gantt chart created using Python’s matplotlib data visualisation library. Image by Andy McDonald
Example Gantt chart created using matplotlib — image by the author.

Often matplotlib has a bit of a reputation for creating boring figures and being awkward to use. However, with some patience, research and extra lines of python code, we can create some unique and very visually appealing figures.

When learning python, matplotlib is often the first data visualisation library that people come across. With a few lines of code, you may end up generating a figure like the one below. Shortly afterwards, many people then move on to learning Seaborn or Plotly and they never look back.

Example of a basic matplotlib scatterplot. Image by the author.

However, there may be times when you want to create a very specific figure, and these high-level libraries are not able to do precisely what you want. Or maybe, you want to have some fun creating unique visualisations. This is where matplotlib comes back into play.

Matplotlib is a low-level library that allows you to modify nearly all of the elements within the figure. This ranges from labels to tick marks, axes, and much more. However, it can be tiresome and awkward to work with, and often you have to resort to using Google and StackOverflow to understand how to modify certain elements.

I don’t hate using some of the higher-level plotting libraries; however, I often find myself limited by some of their functionality, especially when it comes to working with well-log data, and creating figures for publications.

Within this article, we will take a quick look at three unique visualisations that can be generated with matplotlib that you may not have thought possible.

Gantt charts are a popular visualisation tool for tracking the progress of projects. They were first popularised by Henry Gantt in the 1910s, although the precursor to these charts can be traced back to 1896 when they were referred to harmanograms by Karol Adamiecki.

Gantt charts are commonly used in project management and can provide a wealth of information about a project, including:

  • provides an overview of how a project is progressing
  • what tasks are complete
  • what tasks are still to be done
  • how long are tasks supposed to take
  • and much more

Even though a wealth of tools (paid and free) are available for project management, there may be an occasion when you want to create a Gantt Chart directly in Python—for example, displaying a Gantt chart on a Streamlit dashboard.

The example below utilises matplotlib’s horizontal barplot to display each task. Utilising the left parameter, we can start the parts at the expected start dates and make them the full length of the task duration.

We have also coloured each task by its project phase, which could easily be swapped out for task categories or to indicate people assigned to that task.

The red line on the plot indicates the current date and will change position if the code has been rerun at a later date than the previous run.

Example Gantt chart created using Python’s matplotlib data visualisation library. Image by Andy McDonald
Example Gantt chart created using matplotlib—image by the author.

If you want to recreate the above Gantt chart, you can do so with the following code:

import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import datestr2num, DateFormatter, DayLocator
from matplotlib.ticker import AutoMinorLocator
from matplotlib.patches import Patch

# Create dummy date
tasks = ['Task A', 'Task B', 'Task C', 'Task D', 'Task E', 'Task F', 'Task G', 'Task H', 'Task I', 'Task J']
start_dates = ['2023-02-25', '2023-03-10', '2023-03-13', '2023-03-23', '2023-04-01', '2023-04-05', '2023-04-12', '2023-04-20', '2023-04-24', '2023-05-02']
end_dates = ['2023-03-03', '2023-03-17', '2023-03-22', '2023-03-30', '2023-04-07', '2023-04-18', '2023-04-23', '2023-04-25', '2023-05-03', '2023-05-07']

# Setup the dates and calculate durations
start_dates = [datestr2num(d) for d in start_dates]
end_dates = [datestr2num(d) for d in end_dates]

durations = [(end - start) for start, end in zip(start_dates, end_dates)]

fig, ax = plt.subplots(figsize=(15, 8), facecolor='#25253c')

ax.set_facecolor('#25253c')

# Create colours for each task based on categories
colors = ['#7a5195', '#ef5675', '#ffa600']
task_colors = [colors[0]] * 3 + [colors[1]] * 4 + [colors[2]] * 3

# Display the bars
ax.barh(y=tasks, width=durations, left=start_dates,
height=0.8, color=task_colors)

ax.invert_yaxis()

# Setup the x axis labels
ax.set_xlim(start_dates[0], end_dates[-1])

date_form = DateFormatter("%Y-%m-%d")
ax.xaxis.set_major_formatter(date_form)

ax.xaxis.set_major_locator(DayLocator(interval=10))
ax.xaxis.set_minor_locator(AutoMinorLocator(5))
ax.tick_params(axis='x', which='minor', length=2, color='white', labelsize=6)

ax.get_yaxis().set_visible(False)

# Control the colour of the grid for major and minor lines
ax.grid(True, axis='x', linestyle='-', color='#FFFFFF', alpha=0.2, which='major')
ax.grid(True, axis='x', linestyle='-', color='#FFFFFF', alpha=0.05, which='minor')
ax.set_axisbelow(True)

# Add labels for each task. For padding, we can use an f-string and add some extra space
for i, task in enumerate(tasks):
ax.text(start_dates[i], i, f' {task}', ha='left', va='center', color='white', fontsize=12, fontweight='bold')

# Add the current date line
today = datetime.datetime.now().strftime("%Y-%m-%d")
today_num = datestr2num(today)
ax.axvline(today_num, color='red', alpha=0.8)

# Style ticks, labels and colours
ax.tick_params(axis='both', colors='white')

ax.set_xlabel('Date', color='white', fontsize=12)
ax.set_title('Project Schedule', color='white', fontsize=14)

# Hide spines so only bottom is visible
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Create a list of custom patches for the legend
legend_elements = [
Patch(facecolor=colors[0], label='Planning'),
Patch(facecolor=colors[1], label='Development'),
Patch(facecolor=colors[2], label='Testing'),
]

# Add the legend in the top right corner of the plot
ax.legend(handles=legend_elements, loc='upper right',
facecolor='white',
edgecolor='white',
fontsize=10, title='Phases', title_fontsize=12, frameon=True)

plt.show()

Example of a matplotlib donut chart with a figure in the centre. Image by the author.

Donut charts are a simple alternative to the common pie chart. Instead of categories being represented as slices in a pie chart, they are instead represented as arcs on the donut chart. This allows the reader to focus on the length of the arc rather than angles and areas.

Commonly the centre of the donut chart is left blank, but it provides an excellent opportunity to place key information or data. For example, within financial dashboards, we may have a revenue target to reach, representing the whole donut. The portion of the donut that is filled can be used to represent revenue earned, and the centre of the chart can be used to provide the total figure for that earned revenue.

Alternatively, they could be used to monitor progress, with the completed progress represented as a percentage in the centre.

The above donut chart can be created with the following code.

Adding the figures to the centre of the donut is achieved by using matplotlibs text function. This allows us to control the format and position of the text.

Radial bar chart as an alternative to the conventional bar chart. Image by the author.

Often on dashboards or infographics, we have radial bar charts presented. These are visually attractive alternatives to traditional bar charts. Instead of plotting data using the conventional cartesian coordinate system, the bars are represented as rings using the polar coordinate system.

Radial bar charts are visually appealing graphics to include in presentations or posters. However, they do have a few drawbacks. They can be difficult to understand due to our visual system finding ittougher to interpret curved lines compared to straight lines. Additionally, the centre radial bars can be harder to read compared to those further out.

The above radial bar chart can be created using the code below.

From the code, we can see that we have to create two axes, one that uses regular cartesian coordinates (for the background figure) and one that uses polar coordinates.

We can then loop through each category in the dataset and add them as a ring on the chart.

Additionally, choosing appropriate colours for visualisations can be subjective and can be a time-consuming process. If you are looking for resources on how to make the colour selection easier, then I recommend checking out my article on 4 Essential Tools to Help You Select a Colour Palette for Your Data Visualisation.

If you want to understand more about how this figure was constructed, then I check out my article below:

As you can see, matplotlib is not limited to creating simple figures with blue points or lines and no labelling. With a little bit of effort, you can customise the plots to suit your style and needs. Even though there are many other libraries available for creating data visualisations with fewer lines of code, I always find myself coming back to matplotlib to create figures.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment