I would like to visualize a dataset (with legend and color bar). The dataset is a pandas dataframe as follows: X and Y are the values, the year column defines the year of data collection, and the shape column defines the method that is used in data collection. I am wondering how I can plot Y against X while color-coded based on years and markers (and therefore legend) defined by shape in one command such as plot.
df = pd.DataFrame({
'X': [1, 1, 2, 2, 2, 3, 3, 4],
'Y': [3, 4, 1, 6, 7, 8, 8, 5],
'year': [1998, 1999, 1994, 1991, 1999, 1995, 1994, 1992],
'shape': ['o', '^', 'o', '^', 'o', '^', 'o', '^']
})
Of course, I can loop over the shapes and plot per shape (method) and add a color bar separately as well ensuring the min and max of the years in the color bar and legend based on shape. I would like to avoid loop and manual setting of legend or color bar if possible. I am wondering if there is a better way forward. Thank you!
Edit:
I am after sth like this based on the answer provided below by Tranbi, I need both legend and color bar:
1条答案
按热度按时间kb5ga3dv1#
This sounds like a job for seaborn:
Output:
Edit: adding legend and colormap
Edit2: adding method column + changing color palette