Home > Net >  Pandas plot line with different line styles?
Pandas plot line with different line styles?

Time:10-23

I am plotting several lines on the same plot, using the ggplot style. With this style, the lines become all solid lines. So the visibility is not good. How can I change each line to have different styles, e.g., one with dashed lines, or something?

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')

fig,ax = plt.subplots(figsize=(15,5))
ax.set_title('Loss curve', fontsize=15)
ax.set_ylabel('Loss')
ax.set_xlabel('Epoch')
df1.plot.line(ax=ax,x='epoch',y=["train_loss"])
df2.plot.line(ax=ax,x='epoch',y=["train_loss"])
plt.show()

enter image description here

CodePudding user response:

You can use linestyle to change each line with different styles.

Here is an example :

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')

fig,ax = plt.subplots(figsize=(15,5))
ax.set_title('Loss curve', fontsize=15)
ax.set_ylabel('Loss')
ax.set_xlabel('Epoch')

df1 = pd.DataFrame({'epoch' : [10,20,30,40,50,60],
                    'train_loss' : [6,5,4,3,2,1]})

df2 = pd.DataFrame({'epoch' : [10,20,30,40,50,60],
                    'train_loss' : [6.5,5.5,4.5,3.5,2.5,1.5]})

df1.plot.line(ax=ax,x='epoch',y=["train_loss"], 
              linewidth= 1.5, linestyle='-.')
df2.plot.line(ax=ax,x='epoch',y=["train_loss"], linewidth= 1.5,
              linestyle='-')
plt.show()

The above code will show the graph as following : enter image description here

At plt.plot.line(), you can check more styles as solid, dash, dash-dot, dotted, and etc.

  • Related