admin管理员组

文章数量:1289421

I am plotting a graph with date on the x axis and data on the y axis. However the graph is completly wrong and I don't understand why...

df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])
# x axis values
x = df['Date_TrailClean']
# corresponding y axis values
y = df['AdjTotItems']

fig, ax = plt.subplots()
# plotting the points 
ax.plot(x, y)

ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())

ax.set_ylabel(r'Adjusted Total Items')

# function to show the plot
plt.show()

Which produces a graph like this, as if the data is plotted on the wrong axes?

Data can be accessed here:

I am plotting a graph with date on the x axis and data on the y axis. However the graph is completly wrong and I don't understand why...

df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])
# x axis values
x = df['Date_TrailClean']
# corresponding y axis values
y = df['AdjTotItems']

fig, ax = plt.subplots()
# plotting the points 
ax.plot(x, y)

ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())

ax.set_ylabel(r'Adjusted Total Items')

# function to show the plot
plt.show()

Which produces a graph like this, as if the data is plotted on the wrong axes?

Data can be accessed here: https://docs.google/spreadsheets/d/10bf0dEUz5lvBjh4kWH8XXfX0yOzdU8NYjSzQBwNV4bk/edit?usp=sharing

Share Improve this question edited Feb 21 at 9:20 Heather Kay asked Feb 21 at 8:58 Heather KayHeather Kay 911 silver badge8 bronze badges 7
  • You need to first ensure that column is in date-time format. try adding this to pd.to_datetime(df['Date_TrailClean']) and check the result. Also look at x.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y')) will format the dates as "Jan 2025", "Jul 2025", etc., to make it clearer. – newstudent Commented Feb 21 at 9:03
  • Did I not set it as datetime in the first line of my script? – Heather Kay Commented Feb 21 at 9:10
  • if you can share a sample data, it would be helpful – newstudent Commented Feb 21 at 9:13
  • 2 The data will be plot in the order it's given, and your data is probably sorted y-wise. Try sorting your data x-wise instead. But... you seem to have more than one value for a given x, making your data not a function. This means that the shape of the plot will depend on the y order for elements belonging to the same x value. Are you sure a line plot is what you want? – Cincinnatus Commented Feb 21 at 9:20
  • I've added the data to the original question, this is before converting to datetime. There aren't any repeat dates from what I can see – Heather Kay Commented Feb 21 at 9:21
 |  Show 2 more comments

2 Answers 2

Reset to default 2
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


df = pd.read_excel('sample_data.xlsx')
df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])

# Sort x-axis 
df = df.sort_values('Date_TrailClean')
x = df['Date_TrailClean']
y = df['AdjTotItems']


fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y, color='blue', marker='o', label='Adjusted Total Items')
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())

ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))  # e.g., Jan 2025, Jul 2025

plt.xticks(rotation=45)

ax.set_xlabel('Date')
ax.set_ylabel(r'Adjusted Total Items')
ax.set_title('Adjusted Total Items Over Time')
ax.grid(True)
plt.tight_layout()  
plt.show()

As others pointed out, sorting the x-axis data gives you above plot.

Just to post an old style solution…

from matplotlib.pyplot import plot, show, xlabel, ylabel
from csv import reader
from datetime import date

# you have a non-standard date format
toISO8601 = lambda s: '-'.join(reversed(s.split('/')))

data = reader(open('Pippo.csv'))
xlbl, ylbl = next(data)
data = [[date.fromisoformat(toISO8601(d)), float(f)] for d, f in data]
data = sorted(data)
# transpose [[x0,y0],[x1,y1], ...] to [[x0,x1,...],[y0,y1,...]]
# and unpack into x and y
x, y = zip(*data)
plot(x, y)
xlabel(xlbl) ; ylabel(ylbl)
show() 

本文标签: pythonData apparently plotted wrong way on matplotlibStack Overflow