#121 Line chart customization

The graph #120 explains how to create a linechart. This post aims to show how to customize it, feature per feature. To realize the following examples, we first need to import libraries and create data:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df=pd.DataFrame({'x': range(1,11), 'y': np.random.randn(10) })


Custom line color

To custom color, just use the ‘color’ argument! To learn the color syntax, read this page.

plt.plot( 'x', 'y', data=df, color='skyblue')

Note that you can add transparency to the color with the alpha argument (0=transparent, 1=opaque)

plt.plot( 'x', 'y', data=df, color='skyblue', alpha=0.3)
Custom line style

Then you can choose between different line style with the ‘linestyle’ argument:

plt.plot( 'x', 'y', data=df, linestyle='dashed')

The following 4 styles are available:

plt.plot( [1,1.1,1,1.1,1], linestyle='-' , linewidth=4)
plt.text(1.5, 1.3, "linestyle = '-' ", horizontalalignment='left', size='medium', color='C0', weight='semibold')
plt.plot( [2,2.1,2,2.1,2], linestyle='--' , linewidth=4 )
plt.text(1.5, 2.3, "linestyle = '--' ", horizontalalignment='left', size='medium', color='C1', weight='semibold')
plt.plot( [3,3.1,3,3.1,3], linestyle='-.' , linewidth=4 )
plt.text(1.5, 3.3, "linestyle = '-.' ", horizontalalignment='left', size='medium', color='C2', weight='semibold')
plt.plot( [4,4.1,4,4.1,4], linestyle=':' , linewidth=4 )
plt.text(1.5, 4.3, "linestyle = ':' ", horizontalalignment='left', size='medium', color='C3', weight='semibold')
Custom line width

Finally you can custom the line width as well

plt.plot( 'x', 'y', data=df, linewidth=22)

Leave a Reply

Your email address will not be published.