vitalstrike82
vitalstrike82

Reputation: 301

Getting data on matplotllib in bar chart

Currently working to try out matplotlib using object oriented interface. I'm still new to this tool.

This is the end result of the graph (using excel) I want to create using matplotlib.

enter image description here

I have load the table into dataframe which look like this. enter image description here

Below is the code I wrote.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

loaddf = pd.read_excel("C:\\SampleRevenue.xlsx")

#to get the row on number of tickets
count = loaddf.iloc[0]
#to get the total proceeds I get from sellling the ticket
vol = loaddf.iloc[1]
#The profit from tickets after deducting costs
profit = loaddf.iloc[2]

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(str(count), list(loaddf.columns.values))

Somehow this is the graph I received. How do I display the number of tickers in bar form for each month? Intention is Y axis number of tickets and x axis on months enter image description here

This is the count, vol and profit series after using iloc to extract the rows. Do i need to remove the series before I use for plotting? enter image description here

Upvotes: 0

Views: 101

Answers (1)

JohanC
JohanC

Reputation: 80409

What's happening is that read_excel gets really confused when the dataframe is transposed. It expects the first row to be the titles of the columns, and each subsequent row a next entry. Optionally the first column contains the row labels. In that case, you have to add index_col=0 to the parameters of read_excel. If you copy and paste-transpose everything while in Excel, it could work like:

import pandas as pd
import matplotlib.pyplot as plt

loaddf = pd.read_excel("C:\\SampleRevenue_transposed\.xlsx", index_col=0)
loaddf[["Vol '000"]].plot(kind='bar', title ="Bar plot of Vol '000")
plt.show()

If you don't transpose the Excel, the header row gets part of the data, which causes the "no numeric data to plot" message.

Upvotes: 1

Related Questions