3

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

2

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

simple give a list in the style property of the lines, like in

f_int_df = pandas.DataFrame(f_int_lst)
f_int_df['ds_pos'] = -F(q0, s_pos, alpha=alpha, gamma=gamma)
f_int_df.set_index('s')[['sum', 'ds_pos']].plot(
    figsize=(4,2), lw=3, grid=True, 
    style=['-', '--'], 
    title="$\propto\int_{q_0\wedge(q_0-s)}^\infty (1+a)^{-3} (1+a+s)^{-3}\, da$"
)

that gives (focus on the style=['-', '--'] line of code):

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.