Reputation: 367
I have table:
import pandas as pd
df = pd.DataFrame([
("2018-06-10", 10),
("2018-07-10", 25),
("2018-08-10", 40),
("2018-09-10", 52),
("2018-10-10", 65),
("2018-11-10", 78),
("2018-12-10", 98),
("2019-01-10", 122),
("2019-02-10", 132),
("2019-03-10", 150),
("2019-04-10", 165),
("2019-05-10", 177)],
columns=["date", "values"])
I need to plot a bar chart in matplotlib, with the following:
Bars will be located between the 10th day of each month;
Height of each bar will be equal to the difference of neighbouring values of the table.
To make the chart look like this:
Upvotes: 0
Views: 69
Reputation: 98
You want to avoid using "values" as a column name, since I believe this is reserved in pandas for all registers. So if you do df.values you will get more then just that column. You probably also want the dates to be vertical so I added rotation="vertical" in "plt.xticks", but if not you can remove it.
This worked for me, tell me if you have any problems with it.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame([
("2018-06-10", 10),
("2018-07-10", 25),
("2018-08-10", 40),
("2018-09-10", 52),
("2018-10-10", 65),
("2018-11-10", 78),
("2018-12-10", 98),
("2019-01-10", 122),
("2019-02-10", 132),
("2019-03-10", 150),
("2019-04-10", 165),
("2019-05-10", 177)],
columns=["date", "value"])
objects = df.date
y_pos = np.arange(len(objects))
performance = list(df.value.diff().dropna())+[0]
plt.bar(y_pos, performance, 1, align='edge', alpha=0.5)
plt.xticks(y_pos, objects, rotation="vertical")
plt.ylabel('Usage')
plt.title('Programming language usage')
plt.show()
Edited, didn't see the part where you mentioned you needed the difference. I also joined the bars together (which I think you also need?) and pushed the dates to the end.
Upvotes: 1
Reputation: 446
Try this:
df["diff"]=df["values"].diff()
df.plot(kind="bar",x="date",y="diff")
plt.show()
Upvotes: 1