Sep-23-2020, 12:20 AM
I am learning a bit about matplotlib here, their own webpage.
To quote the matplotlib page: "Typically one finds oneself making the same plots over and over again, but with different data sets, which leads to needing to write specialized functions to do the plotting. The recommended function signature is something like:"
What I want to do is add a legend. In the previous example of a linear, quadratic and cubic plot, that was achieved like this:
How can I add label to this function and so display the legend?? I've tried various things, but I just get errors.
To quote the matplotlib page: "Typically one finds oneself making the same plots over and over again, but with different data sets, which leads to needing to write specialized functions to do the plotting. The recommended function signature is something like:"
What I want to do is add a legend. In the previous example of a linear, quadratic and cubic plot, that was achieved like this:
ax.plot(x, x, label='linear') # Plot some data on the axes. ax.plot(x, x**2, label='quadratic') # Plot more data on the axes... ax.plot(x, x**3, label='cubic') # ... and some more. ax.legend() # Add a legend.This code is from their webpage, I just changed it slightly.
How can I add label to this function and so display the legend?? I've tried various things, but I just get errors.
def myApp(): import matplotlib.pyplot as plt import numpy as np def my_plotter(ax, data1, data2, param_dict): """ A helper function to make a graph Parameters ---------- ax : Axes The axes to draw to data1 : array The x data data2 : array The y data param_dict : dict Dictionary of kwargs to pass to ax.plot Returns ------- out : list list of artists added """ # if you don't set the ranges, the range shown will be a little more than the maximum value #ax.set_xlim(0, 10) # set the X range #ax.set_ylim(0, 10) # set the Y range ax.set_xlabel('my X axis') ax.set_ylabel('my Y label') ax.set_title('Some Random Plot') #ax.legend() # Add a legend. ax.grid(True) out = ax.plot(data1, data2, **param_dict) return out data1, data2, data3, data4 = np.random.randn(4, 10) fig, ax = plt.subplots(1, 1) my_plotter(ax, data1, data2, {'marker': 'x'}) plt.show()