1

I have the following dataframe coming from an excel file:

df = pd.read_excel('base.xlsx')

My excel file contains the following columns:

  • data - datetime64[ns]
  • stock- float64
  • demand - float64
  • origem - object

I need to plot a bar chart where the x-axis will be the date and the bars the stock and demand. Blue would be the demand and orange the stock:

blue would be the demand and orange the stock

1
  • 3
    welcome to stackoverflow and congrats on your first question. Make sure you read the guidelines for posting questions. Share information that might help us help you, e.g. code, data, desired output. And remember, post your own efforts as well. Commented Nov 30, 2020 at 12:50

1 Answer 1

1

This can be done with the pandas bar plot function. Note that if there are dates that are not recorded in your dataset (e.g. weekends or national holidays) they will not be automatically displayed with a gap in the bar plot. This is because bar plots in pandas (and other packages) are made primarily for categorical data, as mentioned here and here.

import numpy as np                 # v 1.19.2
import pandas as pd                # v 1.1.3
import matplotlib.pyplot as plt    # v 3.3.2

# Create a random time series with the date as index
# In your case where you are importing your dataset from excel you
# would assign your date column to the df index like this:
rng = np.random.default_rng(123)
days = 7
df = pd.DataFrame(dict(demand = rng.uniform(100, size=days),
                       stock = rng.uniform(100, size=days),
                       origin = np.random.choice(list('ABCD'), days)),
                  index = pd.date_range(start='2020-12-14', freq='D', periods=days))

# Create pandas bar plot
fig, ax = plt.subplots(figsize=(10,5))
df.plot.bar(ax=ax, color=['tab:blue', 'tab:orange'])

# Assign ticks with custom tick labels
# Date format codes for xticklabels:
# https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
plt.xticks(ax.get_xticks(), [ts.strftime('%A') for ts in df.index], rotation=0)
plt.legend(frameon=False)
plt.show()

barplot_2vars

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

Comments

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.