Reputation: 133
I have a script which takes the articles on my website, and associated traffic data such as page views, average time on page etc from Google Analytics and places them into a pandas DataFrame.
I would like to plot the best 5 performing pages as bar chart however the bar plots aren't graphing properly.
The script successfully grabs the GA data and places it into a DataFrame so i'm able to do all my analysis but it fails to properly build a bar plot as shown in the image.
In this example, i've replaced the page names with numbers 1-5 and the page views data is as shown:
['3881', '3707', '2269', '1950', '1812', '1734']
As we can see in the image the bars aren't correctly representing the values.
import pandas as pd
import matplotlib.pyplot as plt
#[GA code is here but deleted for simplicity]
x = ["0", "1","2","3","4","5"]
y = ['3881', '3707', '2269', '1950', '1812', '1734']
plt.bar(x,y,label="Bars 1",color="black")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Bar")
plt.legend()
plt.show()
One would expect a bar plot where the values decrease starting from 3881 down to 1734 with each individual bar correctly drawn. Instead, we get a graph where the first bar is zero and it increases, contradicting the values on the axis.
Upvotes: 2
Views: 14055
Reputation: 5389
You are plotting your data as strings. Change y
to be integers and I think you'll get what you expect
Something like:
plt.bar(x, [int(i) for i in y], label="Bars 1", color="black")
Upvotes: 3