1

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?

2
  • Matplotlib 2.1 indeed sorted the values. This is fixed in matplotlib 2.2 or higher. Commented Nov 17, 2019 at 14:58
  • That's right. Put as an answer please!Thanks Commented Nov 17, 2019 at 15:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.