I am trying to plot a dataset from data.boston.gov ( https://data.boston.gov/dataset/central-library-electricity-usage ) using Anaconda's distribution of Spyder. Original dataset contains more than 2x10^5 instances so I have confined to 2018. Plot won't show.
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('bpl_energy_2018.csv')
plt.plot(data.datetime_measured,data.total_demand_kw)
plt.show()
['datetime_measured','total_demand_kw']
- 0 12-31-18 23:55:00 561
- 1 12-31-18 23:50:00 568
- 2 12-31-18 23:45:00 576
...
- 53690 01-01-18 03:40:00 770
- 53691 01-01-18 03:30:00 813
- 53692 01-01-18 02:55:00 777
[53693 rows x 2 columns]
I think the reason it's not working is because your data is all out of order, so matplotlib doesn't know what to do with the values you're giving it.
Pandas has some built-in plotting features, so you should be able to plot your data just with
data.plot()
plt.show()
The plot then looks like this:
But that is basically just random noise. If you look at the values in the CSV, you'll see that they're not sorted perfectly by time. We can fix this without too much trouble, though:
data.sort_values('datetime_measured', inplace=True)
data.reset_index(drop=True, inplace=True)
If we plot it again, we get this:
.
Since you are plotting a time series, I would recommend using the pandas
built in plotting functions, especially as you already have the data as a DataFrame
.
To keep the datetime format on the xaxis, you just need to tell the .plot()
function which columns to use. For example:
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('bpl_energy_2018.csv')
data.sort_values('datetime_measured', inplace=True)
data.plot('datetime_measured', 'total_demand_kw')
# Rotate and align xtick labels
ax.get_figure().autofmt_xdate()
# make room for tick labels
plt.tight_layout()
plt.show()
Note that I rotated the ticks with ax.get_figure().autofmt_xdate()
and made space for them using tight_layout()
.
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.