4

Either Numpy or Matplotlib is changing the order of my np.array and it's conflicting with my plot. It's causing the months to be out of order while the corresponding data to still be in the same order which is causing the plot to look weird:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
plt.plot(month, f)

plt.xlabel('Month')
plt.ylabel('Temperature')
plt.title('Average Monthly Temperature in Elizabeth City, NC')
plt.show()

This is what i get as output in JupyterNotebook:

3
  • 1
    Matplotlib is sorting it alphabetically before plotting it Commented Jan 24, 2018 at 0:48
  • Oh okay. It's weird that its still using the corresponding data in the right order. Commented Jan 24, 2018 at 0:50
  • Sorry, what I meant to say is: matplotlib is turning the strings into x-coordinates by ordering all the strings, and returning the position of each one. The order of the line is unchanged, and still passes through the months in order Commented Jan 24, 2018 at 1:40

2 Answers 2

6

Since month is a string array, plt.plot() command is sorting it alphabetically. So, we have to use the xticks and then plot it like below to get the strings in the same order as it were in the original array month.

In [16]: f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
    ...: month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
    ...: plt.xticks(range(len(f)), month)
    ...: plt.plot(f)

Plot:

plt with months in alphabetical order

Note: For more customized plots refer: pylab date demo

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

3 Comments

Thanks a bunch. I truly appreciate it :)
Note that this won't make the month of january wider than february to reflect its extra days, unlike @AGN's solution below
@Eric I've added a note at the bottom!
1

You will need to use MonthLocator and set_major_locator as shown here: formatting timeseries x-axis in pandas/matplotlib

Here is my attempt:

import matplotlib.pyplot as plt
import numpy as np
import datetime
f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])

# New stuff:
from matplotlib.dates import MonthLocator, DateFormatter
dates = []
for month in range(1, 13):
    dates.append(datetime.datetime(year=2018, month=month, day=1))
plt.plot(dates, f)
ax = plt.gca()
ax.set_xlim([dates[0], dates[-1]])
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))

plt.xlabel('Month')
plt.ylabel('Temperature')
plt.title('Average Monthly Temperature in Elizabeth City, NC')
plt.show()

3 Comments

Im having trouble trying to implement those with my existing code
@EdselNorwood I have added an example to my answer. Check it out.
Once you have an ax object, you may as well call all the plt methods on it too - ax.xlabel, ax.ylabel, and ax.title

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.