DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Digitize a Graph

How to Digitize a Graph

Sometimes life throws you a curve and you need to draw some conclusions. Here's an easy-to-use Python method to help you. Warning: The following content is graphic.

John Cook user avatar by
John Cook
·
May. 02, 16 · Web Dev Zone · Tutorial
Like (5)
Save
Tweet
8.55K Views

Join the DZone community and get the full member experience.

Join For Free

suppose you have a graph of a function, but you don’t have an equation for it or the data that produced it. how can you reconstruction the function?

there are a lot of software packages to digitize images. for example, web plot digitizer is one you can use online. once you have digitized the graph at a few points, you can fit a spline to the points to approximately reconstruct the function. then as a sanity check, plot your reconstruction to see if it looks like the original. it helps to have the same aspect ratio so you’re not distracted by something that doesn’t matter, and so that differences that do matter are easier to see.

for example, here is a graph from zwicker and fastl’s book on psychoacoustics. it contains many graphs with no data or formulas. this particular one gives the logarithmic transmission factor between free field and the peripheral hearing system.

here’s python code to reconstruct the functions behind these two curves.

import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate 

curve_names = ["free", "diffuse"]
plot_styles = { "free" : 'b-', "diffuse" : 'g:'}

data = {}
for name in curve_names:
    data = np.loadtxt("{}.csv".format(name), delimiter=',')

    x = data[:,0]
    y = data[:,1]
    spline = interpolate.splrep(x, y)
    xnew = np.linspace(0, max(x), 100)
    ynew = interpolate.splev(xnew, spline, der=0)
    plt.plot(xnew, ynew, plot_styles[name])

logical_x_range  = 24    # bark
logical_y_range  = 40    # db
physical_x_range = 7     # inch
physical_y_range = 1.625 # inch

plt.legend(curve_names, loc=2)
plt.xlabel("critical-band rate")
plt.ylabel("attenuation")
plt.xlim((0, logical_x_range))


plt.axes().set_aspect( 
    (physical_y_range/logical_y_range) / 
    (physical_x_range/logical_x_range) )
ax = plt.gca()
ax.get_xaxis().set_ticks([0, 4, 8, 12, 16, 20, 24])
ax.get_yaxis().set_ticks([-10, 0, 10, 20, 30])

plt.show()

here’s the reconstructed graph.

Graph (Unix)

Published at DZone with permission of John Cook, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • Kafka Fail-Over Using Quarkus Reactive Messaging
  • Top Soft Skills to Identify a Great Software Engineer
  • How to Determine if Microservices Architecture Is Right for Your Business?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo