I am trying to plot a line chart in matplotlib with the following code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'col1':['j','c','m','i','s'], 'col2':[614,670,700,740,800]})
plt.plot(df['col1'], df['col2'])
plt.show()
As you can see, the dataframe is sorted in ascending order according to col2
column values. However, at the time of plotting, matplotlib orders x-axis values in alphabetical order, so while the first element of the axis should be j
, with matplotlib's ordering becomes c
. In the real example, this causes great damage to the visualization and understanding of the graph. So how can I make matplotlib respect the Dataframe order?