I have been coding in Python for a number of years now. I've always felt that Matplotlib code takes up a lot more lines of code than it should. I could be wrong.
I have the following function that plots a simple scatter plot graph with two additional solid lines. Is there any way for me to reduce the number of lines to achieve exactly the same outcome? I feel that my code is a little 'chunky'.
dates contains an array of DateTime values in the yyyy-mm-dd H-M-S format
return_values - array of floats
main_label - string
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
plt.style.use('ggplot')
def plot_returns(dates, return_values, ewma_values, main_label):
plt.figure(figsize=(15, 10))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=31))
plt.scatter(dates, return_values, linestyle = '-', color='blue', s = 3, label = "Daily Returns")
plt.plot(dates, ewma_values, linestyle = '-', color='green', label = "EWMA")
plt.gcf().autofmt_xdate()
plt.xlabel('Day', fontsize = 14)
plt.ylabel('Return', fontsize = 14)
plt.title(main_label, fontsize=14)
plt.legend(loc='upper right', facecolor='white', edgecolor='black', framealpha=1, ncol=1, fontsize=12)
plt.xlim(left = min(dates))
plt.show()
dates = pd.date_range(start = '1/1/2018', end = '10/10/2018')
return_values = np.random.random(len(dates))
ewma_values = 0.5 + np.random.random(len(dates))*0.1
plot_returns(dates, return_values, ewma_values, "Example")
