Issue
I have a dataframe that looks like this:
a b c d e
2020-06-01 90955 2814823.0 5422676.0 1135754.0 3716537.0
2020-07-01 100498 3116529.0 5908477.0 1368607.0 3056651.0
2020-08-01 97441 3107987.0 5702994.0 1340020.0 3235909.0
2020-09-01 95916 3073612.0 5803623.0 1275557.0 2951018.0
2020-10-01 94436 3090192.0 5943429.0 1262183.0 2882972.0
2020-11-01 92148 2826019.0 5495001.0 1214844.0 2567679.0
I then plot the dataframe like this:
ax = df.plot(figsize=(8,3))
ax.autoscale(axis='x',tight=True)
ax.legend(loc=3, bbox_to_anchor=(1.0,0.1));
Each column has a different color and the legend is on the side of the figure (which I want).
How can I change the type of line for each column? For example, one is a straight line and another is a dotted line?
Solution
Pass a list format strings as the style
kwarg of df.plot
import pandas as pd
from matplotlib import pyplot as plt
df = pd.DataFrame({'a': {'2020-06-01': 90955, '2020-07-01': 100498,
'2020-08-01': 97441, '2020-09-01': 95916,
'2020-10-01': 94436, '2020-11-01': 92148},
'b': {'2020-06-01': 2814823.0, '2020-07-01': 3116529.0,
'2020-08-01': 3107987.0, '2020-09-01': 3073612.0,
'2020-10-01': 3090192.0, '2020-11-01': 2826019.0},
'c': {'2020-06-01': 5422676.0, '2020-07-01': 5908477.0,
'2020-08-01': 5702994.0, '2020-09-01': 5803623.0,
'2020-10-01': 5943429.0, '2020-11-01': 5495001.0},
'd': {'2020-06-01': 1135754.0, '2020-07-01': 1368607.0,
'2020-08-01': 1340020.0, '2020-09-01': 1275557.0,
'2020-10-01': 1262183.0, '2020-11-01': 1214844.0},
'e': {'2020-06-01': 3716537.0, '2020-07-01': 3056651.0,
'2020-08-01': 3235909.0, '2020-09-01': 2951018.0,
'2020-10-01': 2882972.0, '2020-11-01': 2567679.0}})
line_styles = ['bs-', '^', '--', 'o-']
ax = df.plot(figsize=(8, 3), style=line_styles)
ax.autoscale(axis='x', tight=True)
ax.legend(loc=3, bbox_to_anchor=(1.0, 0.1))
plt.show()
Answered By - Henry Ecker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.