How to make squiggly lines to represent unshown parts of the axis in matplotlib

 In Matplotlib, you can create squiggly lines to represent unshown parts of the axis using the `set_minor_formatter` and `set_major_formatter` methods of the `Axes` object. This allows you to customize the tick labels and tick positions for both minor and major ticks. To create squiggly lines, you can use a custom formatter that generates squiggly lines as tick labels.


Here's an example of how to create squiggly lines on the x-axis using Matplotlib:


```python

import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator, FuncFormatter


# Create some sample data

x = range(1, 11)

y = [2, 4, 6, 8, 10, 8, 6, 4, 2, 0]


# Create a function to format tick labels with squiggly lines

def squiggly_formatter(x, pos):

    if x % 2 == 0:

        return f'~ {x} ~' # Add squiggly lines to even tick labels

    else:

        return f'{x}' # Keep odd tick labels as is


# Create a figure and axes

fig, ax = plt.subplots()


# Set major and minor tick locators

major_locator = MultipleLocator(1)

minor_locator = MultipleLocator(0.5)


ax.xaxis.set_major_locator(major_locator)

ax.xaxis.set_minor_locator(minor_locator)


# Set squiggly label format for major and minor ticks

ax.xaxis.set_major_formatter(FuncFormatter(squiggly_formatter))

ax.xaxis.set_minor_formatter(FuncFormatter(squiggly_formatter))


# Plot the data

ax.plot(x, y)


plt.xlabel('X-axis')

plt.ylabel('Y-axis')


plt.grid(True, which='both', linestyle='--', linewidth=0.5) # Add grid lines with squiggly style


plt.show()

```


In this example, the `squiggly_formatter` function is used to add squiggly lines to even tick labels while keeping odd tick labels as they are. The `FuncFormatter` is used to apply this custom formatting to both major and minor tick labels. You can modify the `squiggly_formatter` function and other parameters to customize the appearance of your squiggly lines according to your preferences.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?