Close Menu
    Trending
    • PatchMatch vs AI Inpainting — Why PatchMatch Still Excels at High Resolution | by Thuan Bui Huy | Aug, 2025
    • This company figured out how to reuse glass wine bottles, and it’s reshaping the Oregon wine industry
    • Retrieval‑Augmented Generation: Building Grounded AI for Enterprise Knowledge | by James Fahey | Aug, 2025
    • Tell Your Story and Share Your Strategies with the $49 Youbooks Tool
    • The Invisible Edge: Why Retail Traders Are Still Losing (and How AI Can Help) | by Neshanth Anand | Aug, 2025
    • Stop Duct-Taping Your Tech Stack Together: This All-in-One Tool Is Hundreds of Dollars Off
    • How Flawed Human Reasoning is Shaping Artificial Intelligence | by Manander Singh (MSD) | Aug, 2025
    • Exaone Ecosystem Expands With New AI Models
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»From Default Python Line Chart to Journal-Quality Infographics | by Vladimir Zhyvov | Dec, 2024
    Artificial Intelligence

    From Default Python Line Chart to Journal-Quality Infographics | by Vladimir Zhyvov | Dec, 2024

    Team_AIBS NewsBy Team_AIBS NewsDecember 30, 2024No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Remodel boring default Matplotlib line charts into beautiful, custom-made visualizations

    Towards Data Science

    Cowl, picture by the Creator

    Everybody who has used Matplotlib is aware of how ugly the default charts appear to be. On this sequence of posts, I’ll share some methods to make your visualizations stand out and mirror your particular person type.

    We’ll begin with a easy line chart, which is broadly used. The primary spotlight will likely be including a gradient fill beneath the plot — a process that’s not solely simple.

    So, let’s dive in and stroll by all the important thing steps of this transformation!

    Let’s make all the required imports first.

    import pandas as pd
    import numpy as np
    import matplotlib.dates as mdates
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    from matplotlib import rcParams
    from matplotlib.path import Path
    from matplotlib.patches import PathPatch

    np.random.seed(38)

    Now we have to generate pattern information for our visualization. We’ll create one thing just like what inventory costs appear to be.

    dates = pd.date_range(begin='2024-02-01', durations=100, freq='D')
    initial_rate = 75
    drift = 0.003
    volatility = 0.1
    returns = np.random.regular(drift, volatility, len(dates))
    charges = initial_rate * np.cumprod(1 + returns)

    x, y = dates, charges

    Let’s test the way it seems with the default Matplotlib settings.

    repair, ax = plt.subplots(figsize=(8, 4))
    ax.plot(dates, charges)
    ax.xaxis.set_major_locator(mdates.DayLocator(interval=30))
    plt.present()
    Default plot, picture by Creator

    Not likely fascination, proper? However we are going to progressively make it trying higher.

    • set the title
    • set basic chart parameters — dimension and font
    • inserting the Y ticks to the fitting
    • altering the principle line shade, type and width
    # Basic parameters
    fig, ax = plt.subplots(figsize=(10, 6))
    plt.title("Every day guests", fontsize=18, shade="black")
    rcParams['font.family'] = 'DejaVu Sans'
    rcParams['font.size'] = 14

    # Axis Y to the fitting
    ax.yaxis.tick_right()
    ax.yaxis.set_label_position("proper")

    # Plotting primary line
    ax.plot(dates, charges, shade='#268358', linewidth=2)

    Basic params utilized, picture by Creator

    Alright, now it seems a bit cleaner.

    Now we’d like so as to add minimalistic grid to the background, take away borders for a cleaner look and take away ticks from the Y axis.

    # Grid
    ax.grid(shade="grey", linestyle=(0, (10, 10)), linewidth=0.5, alpha=0.6)
    ax.tick_params(axis="x", colours="black")
    ax.tick_params(axis="y", left=False, labelleft=False)

    # Borders
    ax.spines["top"].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines["bottom"].set_color("black")
    ax.spines['left'].set_color('white')
    ax.spines['left'].set_linewidth(1)

    # Take away ticks from axis Y
    ax.tick_params(axis='y', size=0)

    Grid added, picture by Creator

    Now we’re including a tine esthetic element — 12 months close to the primary tick on the axis X. Additionally we make the font shade of tick labels extra pale.

    # Add 12 months to the primary date on the axis
    def custom_date_formatter(t, pos, dates, x_interval):
    date = dates[pos*x_interval]
    if pos == 0:
    return date.strftime('%d %b '%y')
    else:
    return date.strftime('%d %b')
    ax.xaxis.set_major_formatter(ticker.FuncFormatter((lambda x, pos: custom_date_formatter(x, pos, dates=dates, x_interval=x_interval))))

    # Ticks label shade
    [t.set_color('#808079') for t in ax.yaxis.get_ticklabels()]
    [t.set_color('#808079') for t in ax.xaxis.get_ticklabels()]

    12 months close to first date, picture by Creator

    And we’re getting nearer to the trickiest second — methods to create a gradient below the curve. Truly there isn’t any such choice in Matplotlib, however we are able to simulate it making a gradient picture after which clipping it with the chart.

    # Gradient
    numeric_x = np.array([i for i in range(len(x))])
    numeric_x_patch = np.append(numeric_x, max(numeric_x))
    numeric_x_patch = np.append(numeric_x_patch[0], numeric_x_patch)
    y_patch = np.append(y, 0)
    y_patch = np.append(0, y_patch)

    path = Path(np.array([numeric_x_patch, y_patch]).transpose())
    patch = PathPatch(path, facecolor='none')
    plt.gca().add_patch(patch)

    ax.imshow(numeric_x.reshape(len(numeric_x), 1), interpolation="bicubic",
    cmap=plt.cm.Greens,
    origin='decrease',
    alpha=0.3,
    extent=[min(numeric_x), max(numeric_x), min(y_patch), max(y_patch) * 1.2],
    facet="auto", clip_path=patch, clip_on=True)

    Gradient added, picture by Creator

    Now it seems clear and good. We simply want so as to add a number of particulars utilizing any editor (I choose Google Slides) — title, spherical border corners and a few numeric indicators.

    Remaining visualization, picture by Creator

    The complete code to breed the visualization is beneath:



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow I trained a Neural Network to Play Tetris Using Reinforcement Learning | by Tim Hanewich | Dec, 2024
    Next Article The Overlooked SEO Trick That Can Skyrocket Your Rankings
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    I Tested TradingView for 30 Days: Here’s what really happened

    August 3, 2025
    Artificial Intelligence

    Tested an AI Crypto Trading Bot That Works With Binance

    August 3, 2025
    Artificial Intelligence

    Tried Promptchan So You Don’t Have To: My Honest Review

    August 3, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    PatchMatch vs AI Inpainting — Why PatchMatch Still Excels at High Resolution | by Thuan Bui Huy | Aug, 2025

    August 4, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    Mastering the Exponential Function in Keras: A Guide with Practical Examples | by Karthik Karunakaran, Ph.D. | Apr, 2025

    April 2, 2025

    DeepSeek R2: AI for Coding & Language

    March 3, 2025

    Transformers For Image Recognition At Scale: A Brief Summary | by Machine Learning With K | Feb, 2025

    February 12, 2025
    Our Picks

    PatchMatch vs AI Inpainting — Why PatchMatch Still Excels at High Resolution | by Thuan Bui Huy | Aug, 2025

    August 4, 2025

    This company figured out how to reuse glass wine bottles, and it’s reshaping the Oregon wine industry

    August 4, 2025

    Retrieval‑Augmented Generation: Building Grounded AI for Enterprise Knowledge | by James Fahey | Aug, 2025

    August 3, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.