0

I have following dataframe:

date       item_1  item_2
01-01-2021  Low     High
02-01-2021  High    Low
02-01-2021  Low     Low
03-01-2021  Medium    High

I want to plot a bar graph such that date as X-axis and items as Y-axis. Now for item_1 on 1st jan bar would go till Low and item_2 would go till High. On 2nd Jan item_1 will go till high but will also put a marker at Low as there is another entry for low for item_1 on the same date. Below is expected result.

Below is expected result

3
  • You should convert the Low, Med, High values to 1,2,3 anyway, and plot that and then change the axes labels. Commented May 20, 2021 at 12:06
  • 1
    @PerroNoob The OP could do this but matplotlib plots categorical data as well. Commented May 20, 2021 at 12:09
  • I tried df.plot(x ='date', y=['item_1'], kind = 'bar') but got TypeError: no numeric data to plot. Commented May 20, 2021 at 12:22

1 Answer 1

2

The easiest approach I can think of is to 'reencode' your categorical (or in your case ordinal) data as numeric. This will allow you to easily plot the data. Once plotted you can adjust the figure's labels to display categories instead of numbers. Using this approach you'll be able to add, remove and reorder your categories as you see fit.

import matplotlib.pyplot as plt
import pandas as pd

values_1 = ['Low', 'High', 'Low', 'Medium']
values_2 = ['High', 'Low', 'Low', 'High']
dates = pd.to_datetime(['2021-01-01', '2021-01-02', '2021-01-02', '2021-01-03' ])
cols = ['date', 'item_1', 'item_2']
df = pd.DataFrame({'dates': dates, 'item_1': values_1, 'item_2': values_2})

# Dict for converting categories to bar chart heights
cat_to_bar = {'Low': 1, 'Medium': 2, 'High': 3}

# Reencode categorical values as numeric
df[['item_1', 'item_2']] = df[['item_1', 'item_2']].replace(cat_to_bar)
# Use dates as our index
df = df.set_index('dates')
df

[out]

dates   item_1  item_2
2021-01-01  1   3
2021-01-02  3   1
2021-01-02  1   1
2021-01-03  2   3

ax = df.plot(kind='bar')
# Force the y-axis to only have ticks at category heights
ax.set_yticks(list(cat_to_bar.values()))
# change y axis labels from numbers to our category names
ax.set_yticklabels(list(cat_to_bar.keys()))

categorical bar chart

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, It worked. Is there any way to add Mark when there are more than on records for an item. for example for item_1 on 2nd Jan there are two records one low and one high I want to capture the Low value too as shown in the image present in question?
You'd need to first decide how you'd like to display that information. For example a number/count above each bar, by the width of the bar, what should happen if there are more than 1 of the same value e.g. 2 'low' values on the same day. When you're clear on what you'd like to display it'll then be easier to write some code to do what you'd like, or to ask another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.