0

I have data that has Dates that I would like to be able to automatically split in a scatterplot and looks like:

Date            Level        Price
2008-01-01      56           11
2008-01-03      10           12
2008-01-05      52           13
2008-02-01      66           14
2008-05-01      20           10
..
2009-01-01      12           11
2009-02-01      70           11
2009-02-05      56           12
..
2018-01-01      56           10
2018-01-11      10           17
..

Only way I know how to tackle this is to just manually select using iloc and eyeball the dates in the dataframe like this:

fig = plt.figure(figsize=(15,10))
ax1 = fig.add_subplot(111)

ax1.scatter(df.iloc[____, 1], df.iloc[_____, 2], s=10, c='r', marker="o", label='2008')
ax1.scatter(df.iloc[____, 1],df.iloc[____, 2], s=10, c='mediumblue', marker="o", label='2009')

.
.
. (for each year I want)

plt.ylabel('Level', fontsize=14)
plt.xlabel('Price', fontsize=14)
plt.legend(loc='upper left', prop={'size': 12});
plt.show()

But this takes a lot of time.

I'd like to automatically loop through each Date's Year and plot different Levels (Y) to Price (X) on colors by that given year and make a legend label for each year.

What would be a good strategy to do this?

HelloToEarth
  • 2,027
  • 3
  • 22
  • 48
  • 2
    I would suggest to make a "year"-column and use it as `c` argument for a single scatter, such that there is no need for a loop. – ImportanceOfBeingErnest Nov 26 '18 at 16:50
  • Is there a way to control the colors for this? And how could I get labels for the legend on each year? – HelloToEarth Nov 26 '18 at 17:00
  • Figured out that setting a list of colors `colors = ['turquoise','orange','red','mediumblue', 'orchid', 'limegreen']` to the color map as `cmap=matplotlib.colors.ListedColormap(colors)` works. I still need help on the label by year portion. – HelloToEarth Nov 26 '18 at 17:09
  • I missed the part about the label. In that case you can indeed loop over the unique years of the dataframe and plot a scatter for each. Or you use a single scatter and then need to create the legend manually. I have an open [PR to simplify](https://github.com/matplotlib/matplotlib/pull/11127) this, but it's still under review. – ImportanceOfBeingErnest Nov 26 '18 at 18:03
  • Essentially the solution for now would be [this one](https://stackoverflow.com/questions/50654620/add-legend-to-scatter-plot-pca/50655866#50655866), to which this could be a duplciate. – ImportanceOfBeingErnest Nov 26 '18 at 18:14

0 Answers0