Coder Social home page Coder Social logo

Comments (4)

has2k1 avatar has2k1 commented on September 24, 2024 2

It is not properly documented. You need use the scaling package mizani to create a transform object. For that you need a transform function and an inverse function. Checkout some of the simpler transforms like sqrt_trans or reverse_trans.

from mizani.transforms import trans, trans_new

# 1.
class my_trans(trans):
    """
    My Transformation
    """
    @staticmethod
    def transform(x):
        return x

    @staticmethod
    def inverse(x):
        return x
# 2.
# OR you could use the trans_new function to create the trans
# class for you.

def my_transform(x):
    return x

def my_inverse(x):
    return x

my_trans = trans_new('my', my_transform, my_inverse, ...)


scale_x_continuous(trans=my_trans, ...)

When you are done, can you post the final class, data and plotting code that shows the guitar neck. It would be great for the documentation. Thanks.

from plotnine.

jonvitale avatar jonvitale commented on September 24, 2024

@has2k1 thanks for the guidance. Here's what I've come up with:

I am working on a guitar scale trainer in Python. The first step of this project is to create a visual representation of the neck of the guitar and the position of fingers as one plays a sequence of notes. After some experimentation, this is what I've come up with using plotnine. It shows a sequence through a "c chord" in standard tuning.

c_chord

The code was a bit tricky, particularly getting the fret spacing correct. With @has2k1 advice, I was able to use the mizani package to transform the x-axis. I followed the formula on this page to actually build the transformation algebra (seems legit).

I plan to put all of this code on my own github page at some point. But I want to complete more of the project. So for now, here's some code to reproduce my c-chord. By the way, I'm a python beginner (more of an R guy), so any advice is welcome.

import math
import pandas as pd
from plotnine import *
from mizani.transforms import trans, trans_new

# Notes: the 0 fret is an open strum, all other frets are played half-way between fret bars.
# The strings are 1:low E, 2: A, 3: D, 4: G, 5: B, 6: E
c_chord = pd.DataFrame({
    'Fret':   [0, 2.5, 1.5, 0, 0.5, 0],
    'String': [1, 2, 3, 4, 5, 6]
})

# adding the sequence based on the number of notes in the chord
c_chord['Sequence'] = list(range(1, 1+len(c_chord['Fret'])))

# create the standard markings for a Stratocaster
markings = pd.DataFrame({
    'Fret':   [2.5, 4.5, 6.5, 8.5, 11.5, 11.5, 14.5, 16.5, 18.5, 20.5],
    'String': [3.5, 3.5, 3.5, 3.5, 2, 5, 3.5, 3.5, 3.5, 3.5]
})

# create a transformation update the x-axis to resemble the narrowing width of frets on a 25.5 inch Strat
def scale_frets(frets):
    return [25.5 - (25.5 / (2 ** (x/12))) for x in frets]


def scale_frets_inverse(frets_trans):
    return [12 * math.log2(25.5/(25.5-y)) for y in frets_trans]


def scale_breaks(limits):
    return [0, 22]


def scale_minor_breaks(domain_trans, limits):
    domain = scale_frets_inverse(domain_trans)
    return scale_frets(list(range(round(domain[0]),round(domain[1]))))


print(c_chord)

trans_frets = trans_new('trans', scale_frets, scale_frets_inverse, scale_breaks, scale_minor_breaks, domain=(0, 22))

# Plot the chord on top of the transformed scales, add markings.
# Note: To make the frets marks and strings look different I use the minor and major breaks independently.
g = (ggplot(c_chord, aes('Fret', 'String')) +
    geom_path(aes(color='Sequence'), size=3) +
    geom_point(aes(color='Sequence'), fill='#FFFFFF', size=3.5) +
    geom_point(data=markings, fill='#000000', size=5) +
    scale_color_continuous(guide=False) +
    scale_x_continuous(trans=trans_frets) +
    scale_y_continuous(breaks=list(range(0, 7)), minor_breaks=list(range(0, 1)), limits=(1, 6)) +
    theme(panel_background = element_rect(fill='#FFDDCC'),
          panel_grid_major = element_line(color='#AA9944', size=2.2),
          panel_grid_minor = element_line(color='#998888', size=1)
    )
)
g.save("c_chord.png", width=14, height=2)

from plotnine.

has2k1 avatar has2k1 commented on September 24, 2024

I am musically illiterate, so

It shows a sequence through a "c chord" in standard tuning

plucks at untuned neurons in my brain. At least, I appreciate the scale manipulation.

Given that transform uses custom methods, it is clearer to declare a class the usual way. On the python style, you can use numpy, e.g np.range(0, 10) instead of list(range(0, 10)) [1].

Here, I have changed a few things:

import numpy as np
import pandas as pd
from plotnine import *

from mizani.transforms import trans,

# Notes: the 0 fret is an open strum, all other frets are played half-way between fret bars.
# The strings are 1:low E, 2: A, 3: D, 4: G, 5: B, 6: E
c_chord = pd.DataFrame({
    'Fret':   [0, 2.5, 1.5, 0, 0.5, 0],
    'String': [1, 2, 3, 4, 5, 6]
})

# adding the sequence based on the number of notes in the chord
c_chord['Sequence'] = list(range(1, 1+len(c_chord['Fret'])))

# create the standard markings for a Stratocaster
markings = pd.DataFrame({
    'Fret':   [2.5, 4.5, 6.5, 8.5, 11.5, 11.5, 14.5, 16.5, 18.5, 20.5],
    'String': [3.5, 3.5, 3.5, 3.5, 2, 5, 3.5, 3.5, 3.5, 3.5]
})

# create a transformation update the x-axis to resemble the narrowing width of frets on a 25.5 inch Strat
class frets_trans(trans):
    number_of_frets = 23               # Including 0
    domain = (0, number_of_frets-1)
    
    @staticmethod
    def transform(x):
        x = np.asarray(x)
        return 25.5 - (25.5 / (2 ** (x/12)))
    
    @staticmethod
    def inverse(x):
        x = np.asarray(x)
        return 12 * np.log2(25.5/(25.5-x))
    
    @classmethod
    def breaks_(cls, limits):
        return cls.domain
    
    @classmethod
    def minor_breaks(cls, major, limits):
        _major = cls.inverse(major)
        # minor = cls.transform(np.arange(*np.round(_major)))  # previous (potentially bad)
        minor = cls.transform(np.linspace(*_major, cls.number_of_frets))
        return minor


# Plot the chord on top of the transformed scales, add markings.
# Note: To make the frets marks and strings look different I use the minor and major breaks independently.
g = (ggplot(c_chord, aes('Fret', 'String')) +
    geom_path(aes(color='Sequence'), size=3) +
    geom_point(aes(color='Sequence'), fill='#FFFFFF', size=3.5) +
    geom_point(data=markings, fill='#000000', size=5) +
    scale_x_continuous(trans=frets_trans) +
    scale_y_continuous(breaks=list(range(0, 7)), minor_breaks=[]) +
    guides(color=False) +
    theme(figure_size=(10, 1.43),
          panel_background = element_rect(fill='#FFDDCC'),
          panel_grid_major = element_line(color='#AA9944', size=2.2),
          panel_grid_minor = element_line(color='#998888', size=1)
    )
)
g

The key difference is how the minor breaks are computed. When you round, you hope that you get there right whole numbers. This may not work when the numbers (number of frets aka domain) change, due to off by one errors.

It is easy to overlook, but this scale transformation demonstrates a use case where the breaks are fixed (I guess it is invaluable for demonstration graphics) I will include a little more explanation in the notebook for the documentation.

[1] I found a bug that prevents scale_y_continuous(breaks=np.arange(0, 7), ...) from working.

from plotnine.

has2k1 avatar has2k1 commented on September 24, 2024

Example added at. It will show up in the gallery for the next release.

from plotnine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.