1

I'm an amateur coder currently working for a local business, and I'm trying to learn Python for data visualization for my own development and to contribute to the business.

What I am trying to plot is various visuals of income for the past year. I'd like to start with just a simple income over time lineplot, and I'm having a few issues. Below is what one of my many attempts has looked like:

Line plot attempt

Here is the code for it:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns

file_url = '.../Downloads/Daily Income Report - Daily Totals.csv'

data = pd.read_csv(file_url, header=0, parse_dates=True)

Date = data['Date']
Income = data['Total']

sns.set_theme()

ax = plt.gca()
#ax.xaxis.set_minor_locator(matplotlib.dates.MonthLocator([1, 7]))
#ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
#ax.tick_params(pad=20)

plt.plot(Date, Income)
plt.show()

As you can see, there are a couple of issues with it. The main one that I want to address is the ticks. Firstly, I don't know where 1970 is coming from. I'm doing this in Spyder, and when I look at the date variable, everything is properly registered, and the oldest date is in 2020.

Logically, it likely has to do with that final block of code following the ax = plt.gca(). I pulled that from another post, from someone who was also trying to visualize a time series with a lot of data points succinctly. They, like me, also have a bunch of data points (I have 300+, which likely would make a very messy graph). Much of the code above has been from me trying to synthesize the solutions from various stack posts/YouTube.

I would appreciate any input as far as figuring out how to visualize my data effectively. And where is that 1970 from!?!?

  • `data.plot(x='Date', y='Total', ax=ax, x_compat=True)`. – BigBen Apr 19 '21 at 23:12
  • thanks! that fixed the 1970 issue. Now as formatting ticks, do you know of any resources? I'm going to try modelling it after this one first: https://www.earthdatascience.org/courses/use-data-open-source-python/use-time-series-data-in-python/date-time-types-in-pandas-python/customize-dates-matplotlib-plots-python/ – roseinivory Apr 20 '21 at 00:13
  • See [this](https://matplotlib.org/stable/gallery/ticks_and_spines/date_concise_formatter.html#sphx-glr-gallery-ticks-and-spines-date-concise-formatter-py). – r-beginners Apr 20 '21 at 02:08

0 Answers0