-1

I am wondering how I can make the following type of plot in Python (preferably matplotlib):

enter image description here

I would like four categories along the y-axis, and then the dates along the x-axis just as in the figure.

I have a CSV file with two columns [category], [date]. The date format is: dd-mm-yyy.

Extract:

category1,05-01-2020

category1,02-02-2020

category3,06-03-2020

category2,12-04-2020

etc...

Help will be appreciated!

malhel
  • 61
  • 7

1 Answers1

0

You can simply plot the categories vs. the dates as is. For the color code, you need to convert the categories to individual numbers, which can be easily achieved using pandas Categorical data type.

d = """category1,05-01-2020
category1,02-02-2020
category3,06-03-2020
category2,12-04-2020"""
df = pd.read_csv(StringIO(d), sep=',', parse_dates=[1], header=None, names=['category','date'])


fig, ax = plt.subplots()
ax.scatter(df['date'],df['category'], marker='s', c=df['category'].astype('category').cat.codes, cmap='tab10')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Thank you very much for response. I have an issue with the xaxis. It displays all dates, but I would like it to only show dates at a certain interval. Example: Jan 2010, Jun 2010, Jan 2011, Jun 2011 etc – malhel May 01 '20 at 13:16
  • Also, how can I manually choose the color of the four categories? – malhel May 01 '20 at 15:30
  • For your first question https://stackoverflow.com/questions/25538520/change-tick-frequency-on-x-time-not-number-frequency-in-matplotlib – Diziet Asahi May 03 '20 at 11:36
  • For your second question https://stackoverflow.com/questions/53360879/create-a-discrete-colorbar-in-matplotlib – Diziet Asahi May 03 '20 at 11:38