简体   繁体   中英

How to properly plot bar chart with matplotlib?

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM