Function calls via plt have more visual ambiguity, and need to infer the current figure and axes being explicit is a better idea. It's easy, and preferable, to have local references to your actual figure and axes upon creation, and to use methods bound to those specific objects instead of plt. What if someone else wants to import and reuse parts of your code? Do not enforce a style in the global namespace - only call that from a routine in the application.Said another way, this is not code golf, and not all short code is good code. Your current push - rather than to shed a line or two - should be to increase static testability, maintainability and structure. Should, in isolation, not be your overriding concern, and your code is about as minimally chunky as matplotlib will allow. Is there any way for me to reduce the number of lines to achieve exactly the same outcome? Plot_returns(dates, return_values, ewma_values, "Example") Return_values = np.random.random(len(dates))Įwma_values = 0.5 + np.random.random(len(dates))*0.1 Plt.legend(loc='upper right', facecolor='white', edgecolor='black', framealpha=1, ncol=1, fontsize=12)ĭates = pd.date_range(start = '', end = '') Plt.plot(dates, ewma_values, linestyle = '-', color='green', label = "EWMA") Plt.scatter(dates, return_values, linestyle = '-', color='blue', s = 3, label = "Daily Returns") Plt.gca().t_major_locator(mdates.DayLocator(interval=31)) Plt.gca().t_major_formatter(mdates.DateFormatter('%Y-%m')) 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'.ĭates contains an array of DateTime values in the yyyy-mm-dd H-M-S formatĭef plot_returns(dates, return_values, ewma_values, main_label): I have the following function that plots a simple scatter plot graph with two additional solid lines. I've always felt that Matplotlib code takes up a lot more lines of code than it should. I have been coding in Python for a number of years now.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |