Home > Enterprise >  Putting data from MongoDB into a chart in python (using pandas dataframe)
Putting data from MongoDB into a chart in python (using pandas dataframe)

Time:01-14

I Have a database in MongoDB that shows solar energy production from the year 2019. I imported this in a collection with a json file. I now have to show this data on a chart in python using the plot() function. I read out the data from my MongoDB and put it in a pandas DataFrame.

The issue is that I get a TypeError saying "no numeric data to plot". I assume this is because the data in my dataframe is still in text form and not actual data. So I somehow need to convert this to numeric data and date times.

This is my current code:

import pandas as pd
import matplotlib.pyplot as plt
import pymongo

my_client = pymongo.MongoClient("mongodb://localhost:27017/")
my_db = my_client["ExamenVoorbeeld"]
my_collection = my_db["Opbrengst_zonnepanelen"]

lst_power_production = list(my_collection.find(filter={}, projection={"_id": 0, "Production": 1, "DateTime": 1}))

df_mongo = pd.DataFrame(lst_power_production)

print(df_mongo)

df_mongo.plot(x="DateTime", y = "Production", xlabel="date", ylabel="production (kWh)", figsize=(20,10), title="Solar energy production in 2019")

Extra screenshot attached of my database. Database

CodePudding user response:

You can try changing the datatype of the dataframe columns like:
df_mongo['Production'] = df_mongo['Production'].astype(int)
df_mongo['DateTime'] = pd.to_datetime(df_mongo['DateTime'])

  •  Tags:  
  • Related