Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:13:06+00:00 2026-05-27T17:13:06+00:00

I want to get visualized statistics from my data in mongodb using matplotlib, but

  • 0

I want to get visualized statistics from my data in mongodb using matplotlib, but the way I’m using now is really weird.

I queried the mongodb 30 times for getting day-by-day data, which is already slow and dirty, especially when I’m getting the result from somewhere else instead of on the server. I wonder if there is a better/clean way to get hour-by-hour, day-by-day, month-by-month and year-by-year statistics?

Here is some code I’m using now(get day-by-day statistics):

from datetime import datetime, date, time, timedelta
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from my_conn import my_mongodb

t1 = []
t2 = []
today = datetime.combine(date.today(), time())
with my_mongodb() as m:
    for i in range(30):
        day = today - timedelta(days = i)
        t1 = [m.data.find({"time": {"$gte": day, "$lt": day + timedelta(days = 1)}}).count()] + t1
        t2 = [m.data.find({"deleted": 0, "time": {"$gte": day, "$lt": day + timedelta(days = 1)}}).count()] + t2

x = range(30)
N = len(x)

def format_date(x, pos=None):
    day = today - timedelta(days = (N - x - 1))
    return day.strftime('%m/%d')

plt.bar(range(len(t1)), t1, align='center', color="#4788d2") #All
plt.bar(range(len(t2)), t2, align='center', color="#0c3688") #Not-deleted

plt.xticks(range(len(x)), [format_date(i) for i in x], size='small', rotation=30)
plt.grid(axis = "y")

plt.show()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:13:07+00:00Added an answer on May 27, 2026 at 5:13 pm

    Thanks to @Blubber, I’ve now found a way that is better to handle this purpose using Map/Reduce.

    The fetching data part has been re-written to:

    from dateutil import parser
    parse_time = lambda s: parser.parse(s, ignoretz = True)
    
    func_map = """
    function() {
        if (this.hasOwnProperty("time"))
            emit(this.time.getUTCFullYear() + "/" + (this.time.getUTCMonth() + 1) + "/" + this.time.getUTCDate(),
            {
                count: 1,
                not_deleted: (1 - this.deleted)
            });
    }
    """
    
    func_reduce = """
    function(key, values) {
        var result = {count: 0, not_deleted: 0};
    
        values.forEach(function(value) {
            result.count += value.count;
            result.not_deleted += value.not_deleted;
        });
    
        return result;
    }
    """
    
    with my_mongo() as m:
        result = m.data.inline_map_reduce(func_map, func_reduce)
        dataset = {parse_time(day['_id']): day['value']['not_deleted'] for day in result}
        dataset2 = {parse_time(day['_id']): day['value']['count'] for day in result}
    

    Since I’m quite new to JS, there must be some way better to write those JS functions 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the base 10 logarithm of a Fixnum using Ruby, but
I want to get started doing some game development using Microsoft's XNA. Part of
I want to get a list of files in a directory, but I want
i want get string from one view controller to another view controller i done
I want get the time used for a case so I can create an
I want get all of the Geom objects that are related to a certain
I want to get the MD5 Hash of a string value in SQL Server
I want to get my databases under version control. I'll always want to have
I want to get an overview of files that are updated in TFS (that
I want to get a files these attributes as integer values.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.