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 | Show 2 more comments2 Answers
Reset to default 2import 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
版权声明:本文标题:python - Data apparently plotted wrong way on matplotlib - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741396101a2376388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pd.to_datetime(df['Date_TrailClean'])
and check the result. Also look atx.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