matplotlib/pandas: put line label along the plotted lines in time series plot

  • Last Update :
  • Techknowledgy :

E.g.

import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.linspace(0, 20, 200)
y = np.cos(x) + 0.5 * np.sin(3 * x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.annotate(r '$y = cos(x) + \frac{sin(3x)}{2}$', xy = (x[70], y[70]),
   xytext = (20, 10), textcoords = 'offset points', va = 'center',
   arrowprops = dict(arrowstyle = '->'))
plt.show()

Suggestion : 2

Updated: September 11, 2020,Time period B: 2013-08-01 to 2013-11-01,Time period A: 2012-08-01 to 2012-11-01

# Import necessary packages
import os
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates
import DateFormatter
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting
import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale = 1.5, style = "whitegrid")
# Download the data
data = et.data.get_data('colorado-flood')
# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file with daily precip
file_path = os.path.join("data", "colorado-flood",
   "precipitation",
   "805325-precip-dailysum-2003-2013.csv")
# Import data using datetime and no data value
precip_2003_2013_daily = pd.read_csv(file_path,
   parse_dates = ['DATE'],
   index_col = ['DATE'],
   na_values = ['999.99'])
# Subset data to June - Aug 2005
precip_june_aug_2005 = precip_2003_2013_daily['2005-06-01': '2005-08-31']

precip_june_aug_2005.head()
# Create figure and plot space
fig, ax = plt.subplots(figsize = (12, 12))

# Add x - axis and y - axis
ax.bar(precip_june_aug_2005.index.values,
   precip_june_aug_2005['DAILY_PRECIP'],
   color = 'purple')

# Set title and labels
for axes
ax.set(xlabel = "Date",
   ylabel = "Precipitation (inches)",
   title = "Daily Total Precipitation\nJune - Aug 2005 for Boulder Creek")

plt.show()

Suggestion : 3

If we want to create a line plot instead of the scatter plot, we will have to set linestyle=’solid’ in plt.plot_date(). We can also change the markers.,In this tutorial we will learn to create a scatter plot of time series data in Python using matplotlib.pyplot.plot_date(). We will use Pandas Dataframe to extract the time series data from a CSV file using pandas.read_csv().,We will be using seaborn style to create scatter plot of the time series data. Finally, we will be passing dates and values to plt.plot_date() method and call plt.show() to plot.,We will be formatting the date in our time series plot by using dates from matplotlib. We will be passing a python format string , as we would have passed to strftime to format the date in our time series plot.

We will be using seaborn style to create scatter plot of the time series data. Finally, we will be passing dates and values to plt.plot_date() method and call plt.show() to plot.

# plot_time_series.py

import matplotlib.pyplot as plt
from datetime
import datetime, timedelta
plt.style.use('seaborn')

dates = [
   datetime(2019, 8, 21),
   datetime(2019, 8, 22),
   datetime(2019, 8, 23),
   datetime(2019, 8, 24),
   datetime(2019, 8, 25),
   datetime(2019, 8, 26),
   datetime(2019, 8, 27),
]

y = [0, 1, 2, 3, 4, 5, 6]

plt.plot_date(dates, y)
plt.tight_layout()
plt.show()

If we want to create a line plot instead of the scatter plot, we will have to set linestyle=’solid’ in plt.plot_date(). We can also change the markers.

# plot_time_series.py

plt.plot_date(dates, y, linestyle = 'solid')

Sometimes, we are working with a lot of dates and showing them horizontally won’t be a good idea in that case. So, we can also change the alignment of the dates on x-axis of time series plot by using autofmt_xdate() on plt.gcf().

# plot_time_series.py

plt.gcf().autofmt_xdate

Currently, we were using hard-fed example data to plot the time series. Now we will be grabbing a real csv file of bitcoin prices from here and then create a time series plot from that CSV file in Python using Matplotlib. So, now we have the time series data in CSV file called ‘plot_time_series.csv’. Let us plot this time series data. We will be using pandas’ read_csv method to plot the time series data:-

# plot_time_series.py

data = pd.read_csv('plot_time_series.csv')
price_date = data['Date']
price_close = data['Close']
plt.plot_date(price_date, price_close, linestyle = 'solid')

One thing to note here is that the dates on x-axis are shown as strings and not the dates so DateFormatter won’t work on that. To make them date objects, we will be using pandas.to_datetime.

# plot_time_series.py

import matplotlib.pyplot as plt
from datetime
import datetime, timedelta
from matplotlib
import dates as mpl_dates
import pandas as pd
plt.style.use('seaborn')

data = pd.read_csv('plot_time_series.csv')
data['Date'] = pd.to_datetime(data['Date'])
data.sort_values('Date', inplace = True)
price_date = data['Date']
price_close = data['Close']
plt.plot_date(price_date, price_close, linestyle = 'solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%d-%m-%Y')
plt.gca().xaxis.set_major_formatter(date_format)
plt.tight_layout()
plt.title('Bitcoin Prices')
plt.xlabel('Date')
plt.ylabel('Closing')
plt.show()

Suggestion : 4

pandas.DataFrame.plot.line , pandas.DataFrame.plot ,pandas.DataFrame.plot.pie,pandas.DataFrame.plot.kde

>>> s = pd.Series([1, 3, 2])
>>> s.plot.line()
<AxesSubplot:ylabel='Density'>
>>> df = pd.DataFrame({
      ...'pig': [20, 18, 489, 675, 1776],
      ...'horse': [4, 25, 281, 600, 1900]
         ...
   }, index = [1990, 1997, 2003, 2009, 2014]) >>>
   lines = df.plot.line()
>>> axes = df.plot.line(subplots=True)
>>> type(axes)
<class 'numpy.ndarray'>
>>> axes = df.plot.line(
   ...subplots = True, color = {
      "pig": "pink",
      "horse": "#742802"
   }
   ...)
>>> lines = df.plot.line(x = 'pig', y = 'horse')