Object definitions for graphing and plotting Stream objects. The Graph object subclasses abstract fundamental graphing archetypes using the matplotlib library. The Plot object subclasses provide reusable approaches to graphing data and structures in Stream objects.
Given a stream and any keyword configuration arguments, create and display a plot. Note: plots requires matplotib to be installed. Plot methods can be specified as additional arguments or by keyword. Two keyword arguments can be given: format and values. If positional arguments are given, the first is taken as format and the rest are collected as values. If format is the class name, that class is collected. Additionally, every PlotStream subclass defines one format string and a list of values strings. The format parameter defines the type of Graph (e.g. scatter, histogram, colorGrid). The values list defines what values are graphed (e.g. quarterLength, pitch, pitchClass). If a user provides a format and one or more values strings, a plot with the corresponding profile, if found, will be generated. If not, the first Plot to match any of the defined specifiers will be created. In the case of PlotWindowedAnalysis subclasses, the DiscreteAnalysis subclass indentifiers list is added to the Plot’s values list. Available plots include the following: PlotHistogramPitchSpace PlotHistogramPitchClass PlotHistogramQuarterLength PlotScatterPitchSpaceQuarterLength PlotScatterPitchClassQuarterLength PlotScatterPitchClassOffset PlotScatterPitchSpaceDynamicSymbol PlotHorizontalBarPitchSpaceOffset PlotHorizontalBarPitchClassOffset PlotScatterWeightedPitchSpaceQuarterLength PlotScatterWeightedPitchClassQuarterLength PlotScatterWeightedPitchSpaceDynamicSymbol Plot3DBarsPitchSpaceQuarterLength PlotWindowedKrumhanslSchmuckler PlotWindowedKrumhanslKessler PlotWindowedAardenEssen PlotWindowedSimpleWeights PlotWindowedBellmanBudge PlotWindowedTemperleyKostkaPayne PlotWindowedAmbitus
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> s.plot('histogram', 'pitch')
>>> s = corpus.parse('bach/bwv57.8')
>>> s.plot('pianoroll')
Inherits from: PlotHistogram, PlotStream
A histogram of pitch space.
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotHistogramPitchSpace(s)
>>> p.id
'histogram-pitch'
>>> p.process() # with defaults and proper configuration, will open graph
PlotHistogramPitchSpace attributes
Attributes without Documentation: values
Attributes inherited from PlotHistogram: format
PlotHistogramPitchSpace properties
Properties inherited from PlotStream: id
PlotHistogramPitchSpace methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotHistogram, PlotStream
A histogram of pitch class
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotHistogramPitchClass(s)
>>> p.id
'histogram-pitchClass'
>>> p.process() # with defaults and proper configuration, will open graph
PlotHistogramPitchClass attributes
Attributes without Documentation: values
Attributes inherited from PlotHistogram: format
PlotHistogramPitchClass properties
Properties inherited from PlotStream: id
PlotHistogramPitchClass methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotHistogram, PlotStream
A histogram of pitch class
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotHistogramQuarterLength(s)
>>> p.id
'histogram-quarterLength'
>>> p.process() # with defaults and proper configuration, will open graph
PlotHistogramQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from PlotHistogram: format
PlotHistogramQuarterLength properties
Properties inherited from PlotStream: id
PlotHistogramQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Krumhansl-Schmuckler analysis routine. See KrumhanslSchmuckler for more details.
>>> from music21 import *
>>> s = corpus.parse('bach/bwv66.6')
>>> p = graph.PlotWindowedKrumhanslSchmuckler(s.parts[0])
>>> p.process() # with defaults and proper configuration, will open graph
PlotWindowedKrumhanslSchmuckler attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedKrumhanslSchmuckler properties
Properties inherited from PlotStream: id
PlotWindowedKrumhanslSchmuckler methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of basic pitch span.
>>> from music21 import *
>>> s = corpus.parse('bach/bwv66.6')
>>> p = graph.PlotWindowedAmbitus(s.parts[0])
>>> p.process() # with defaults and proper configuration, will open graph
PlotWindowedAmbitus attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedAmbitus properties
Properties inherited from PlotStream: id
PlotWindowedAmbitus methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotScatter, PlotStream
A scatter plot of pitch space and quarter length
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotScatterPitchSpaceQuarterLength(s)
>>> p.id
'scatter-pitch-quarterLength'
>>> p.process()
PlotScatterPitchSpaceQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from PlotScatter: format
PlotScatterPitchSpaceQuarterLength properties
Properties inherited from PlotStream: id
PlotScatterPitchSpaceQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatter, PlotStream
A scatter plot of pitch class and quarter length
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotScatterPitchClassQuarterLength(s)
>>> p.id
'scatter-pitchClass-quarterLength'
>>> p.process()
PlotScatterPitchClassQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from PlotScatter: format
PlotScatterPitchClassQuarterLength properties
Properties inherited from PlotStream: id
PlotScatterPitchClassQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatter, PlotStream
A scatter plot of pitch class and offset
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotScatterPitchClassOffset(s)
>>> p.id
'scatter-pitchClass-offset'
>>> p.process()
PlotScatterPitchClassOffset attributes
Attributes without Documentation: values
Attributes inherited from PlotScatter: format
PlotScatterPitchClassOffset properties
Properties inherited from PlotStream: id
PlotScatterPitchClassOffset methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatter, PlotStream
A graph of dynamics used by pitch space.
>>> from music21 import *
>>> s = corpus.parse('schumann/opus41no1', 2)
>>> p = graph.PlotScatterPitchSpaceDynamicSymbol(s)
>>> p.process()
PlotScatterPitchSpaceDynamicSymbol attributes
Attributes without Documentation: values
Attributes inherited from PlotScatter: format
PlotScatterPitchSpaceDynamicSymbol properties
Properties inherited from PlotStream: id
PlotScatterPitchSpaceDynamicSymbol methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotHorizontalBar, PlotStream
A graph of events, sorted by pitch space, over time
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotHorizontalBarPitchSpaceOffset(s)
>>> p.process() # with defaults and proper configuration, will open graph
PlotHorizontalBarPitchSpaceOffset attributes
Attributes without Documentation: values
Attributes inherited from PlotHorizontalBar: format
PlotHorizontalBarPitchSpaceOffset properties
Properties inherited from PlotStream: id
PlotHorizontalBarPitchSpaceOffset methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotHorizontalBar, PlotStream
A graph of events, sorted by pitch class, over time
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotHorizontalBarPitchClassOffset(s)
>>> p.process() # with defaults and proper configuration, will open graph
PlotHorizontalBarPitchClassOffset attributes
Attributes without Documentation: values
Attributes inherited from PlotHorizontalBar: format
PlotHorizontalBarPitchClassOffset properties
Properties inherited from PlotStream: id
PlotHorizontalBarPitchClassOffset methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatterWeighted, PlotStream
A graph of event, sorted by pitch, over time
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotScatterWeightedPitchSpaceQuarterLength(s)
>>> p.process() # with defaults and proper configuration, will open graph
PlotScatterWeightedPitchSpaceQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from PlotScatterWeighted: format
PlotScatterWeightedPitchSpaceQuarterLength properties
Properties inherited from PlotStream: id
PlotScatterWeightedPitchSpaceQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatterWeighted, PlotStream
A graph of event, sorted by pitch class, over time.
>>> from music21 import *
>>> s = corpus.parse('bach/bwv57.8')
>>> p = graph.PlotScatterWeightedPitchClassQuarterLength(s)
>>> p.process() # with defaults and proper configuration, will open graph
PlotScatterWeightedPitchClassQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from PlotScatterWeighted: format
PlotScatterWeightedPitchClassQuarterLength properties
Properties inherited from PlotStream: id
PlotScatterWeightedPitchClassQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotScatterWeighted, PlotStream
A graph of dynamics used by pitch space.
>>> from music21 import *
>>> s = corpus.parse('schumann/opus41no1', 2)
>>> p = graph.PlotScatterWeightedPitchSpaceDynamicSymbol(s)
>>> p.process() # with defaults and proper configuration, will open graph
PlotScatterWeightedPitchSpaceDynamicSymbol attributes
Attributes without Documentation: values
Attributes inherited from PlotScatterWeighted: format
PlotScatterWeightedPitchSpaceDynamicSymbol properties
Properties inherited from PlotStream: id
PlotScatterWeightedPitchSpaceDynamicSymbol methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: Plot3DBars, PlotStream
A scatter plot of pitch and quarter length
>>> from music21 import *
>>> from music21.musicxml import testFiles
>>> s = converter.parse(testFiles.mozartTrioK581Excerpt)
>>> p = graph.Plot3DBarsPitchSpaceQuarterLength(s)
>>> p.id
'3dBars-pitch-quarterLength'
>>> p.process() # with defaults and proper configuration, will open graph
Plot3DBarsPitchSpaceQuarterLength attributes
Attributes without Documentation: values
Attributes inherited from Plot3DBars: format
Plot3DBarsPitchSpaceQuarterLength properties
Properties inherited from PlotStream: id
Plot3DBarsPitchSpaceQuarterLength methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
A music21.graph.Graph is an abstract object that represents a graph or plot, automating the creation and configuration of this graph in matplotlib. It is a low-level object that most music21 users do not need to call directly but because it most graphs will take keyword arguments that specify the look of graphs, they are important to know about. The keyword arguments can be provided for configuration are: alpha (which describes how transparent elements of the graph are), colorBackgroundData, colorBackgroundFigure, colorGrid, title, doneAction (see below), figureSize, colors, tickFontSize, titleFontSize, labelFontSize, fontFamily. Graph objects do not manipulate Streams or other music21 data; they only manipulate raw data formatted for each Graph subclass, hence why it is unlikely that people will call this class directly. The doneAction argument determines what happens after the graph has been processed. Currently there are three options, ‘write’ creates a file on disk (this is the default), while ‘show’ opens an interactive GUI browser. The third option, None, does the processing but does not write any output.
>>> a = Graph(title='a graph of some data to be given soon', tickFontSize = 9)
>>> a.setData(['some', 'arbitrary', 'data', 14, 9.04, None])
Graph attributes
Attributes without Documentation: fontFamily, xTickLabelVerticalAlignment, xTickLabelHorizontalAlignment, doneAction, title, colorBackgroundFigure, colors, tickFontSize, colorGrid, figureSize, colorBackgroundData, axisKeys, grid, xTickLabelRotation, titleFontSize, alpha, labelFontSize, data, dpi, axis
Graph methods
- done(fp=None)¶
- Implement the desired doneAction, after data processing
- process()¶
- process data and prepare plot
- setAxisLabel(axisKey, label)¶
- No documentation.
- setAxisRange(axisKey, valueRange, pad=False)¶
- No documentation.
- setData(data)¶
- No documentation.
- setDoneAction(action)¶
- sets what should happen when the graph is created (see docs above) default is ‘write’.
- setFigureSize(figSize)¶
- Set the figure size as an x,y pair. Scales all graph components but because of matplotlib limitations (esp. on 3d graphs) no all labels scale properly.
- setTicks(axisKey, pairs)¶
- pairs are positions and labels
- setTitle(title)¶
- No documentation.
- show()¶
- Calls the show() method of the matplotlib plot. For most matplotlib back ends, this will open a GUI window with the desired graph.
- write(fp=None)¶
- Writes the graph to a file. If no file path is given, a temporary file is used.
Inherits from: Graph
Graph multiple parallel bar graphs in 3D. This draws bars with polygons, a temporary alternative to using Graph3DBars, above. Note: Due to matplotib issue Axis ticks do not seem to be adjustable without distorting the graph.
>>> from music21 import *
>>> g = graph.Graph3DPolygonBars()
>>> data = {1:[], 2:[], 3:[]}
>>> for i in range(len(data.keys())):
... q = [(x, random.choice(range(10*(i+1)))) for x in range(20)]
... data[data.keys()[i]] = q
>>> g.setData(data)
>>> g.process()
Graph3DPolygonBars attributes
Attributes without Documentation: barWidth
Attributes inherited from Graph: fontFamily, xTickLabelVerticalAlignment, xTickLabelHorizontalAlignment, doneAction, title, colorBackgroundFigure, colors, tickFontSize, colorGrid, figureSize, colorBackgroundData, axisKeys, grid, xTickLabelRotation, titleFontSize, alpha, labelFontSize, data, dpi, axis
Graph3DPolygonBars methods
- process()¶
- No documentation.
Methods inherited from Graph: done(), setAxisLabel(), setAxisRange(), setData(), setDoneAction(), setFigureSize(), setTicks(), setTitle(), show(), write()
Inherits from: Graph
Grid of discrete colored “blocks” to visualize results of a windowed analysis routine. Data is provided as a list of lists of colors, where colors are specified as a hex triplet, or the common HTML color codes, and based on analysis-specific mapping of colors to results.
>>> from music21 import *
>>> g = graph.GraphColorGrid()
>>> data = [['#55FF00', '#9b0000', '#009b00'], ['#FFD600', '#FF5600'], ['#201a2b', '#8f73bf', '#a080d5', '#403355', '#999999']]
>>> g.setData(data)
>>> g.process()
Inherits from: Graph
Grid of discrete colored “blocks” where each block can be labeled Data is provided as a list of lists of colors, where colors are specified as a hex triplet, or the common HTML color codes, and based on analysis-specific mapping of colors to results.
>>> from music21 import *
>>> g = graph.GraphColorGridLegend()
>>> data = []
>>> data.append(('Major', [('C#', '#00AA55'), ('D-', '#5600FF'), ('G#', '#2B00FF')]))
>>> data.append(('Minor', [('C#', '#004600'), ('D-', '#00009b'), ('G#', '#00009B')]))
>>> g.setData(data)
>>> g.process()
Inherits from: Graph
Graph the count of on or more elements in vertical bars Data set is simply a list of x and y pairs, where there is only one of each x value, and y value is a list of values
>>> from music21 import *
>>> import random
>>> g = graph.GraphGroupedVerticalBar()
>>> data = [('bar%s' % x, {'a':3,'b':2,'c':1}) for x in range(10)]
>>> data
[('bar0', {'a': 3, 'c': 1, 'b': 2}), ('bar1', {'a': 3, 'c': 1, 'b': 2}), ('bar2', {'a': 3, 'c': 1, 'b': 2}), ('bar3', {'a': 3, 'c': 1, 'b': 2}), ('bar4', {'a': 3, 'c': 1, 'b': 2}), ('bar5', {'a': 3, 'c': 1, 'b': 2}), ('bar6', {'a': 3, 'c': 1, 'b': 2}), ('bar7', {'a': 3, 'c': 1, 'b': 2}), ('bar8', {'a': 3, 'c': 1, 'b': 2}), ('bar9', {'a': 3, 'c': 1, 'b': 2})]
>>> g.setData(data)
>>> g.process()
GraphGroupedVerticalBar attributes
Attributes without Documentation: roundDigits
Attributes inherited from Graph: fontFamily, xTickLabelVerticalAlignment, xTickLabelHorizontalAlignment, doneAction, title, colorBackgroundFigure, colors, tickFontSize, colorGrid, figureSize, colorBackgroundData, axisKeys, grid, xTickLabelRotation, titleFontSize, alpha, labelFontSize, data, dpi, axis
GraphGroupedVerticalBar methods
- labelBars(ax, rects)¶
- No documentation.
- process()¶
- No documentation.
Methods inherited from Graph: done(), setAxisLabel(), setAxisRange(), setData(), setDoneAction(), setFigureSize(), setTicks(), setTitle(), show(), write()
Inherits from: Graph
Graph the count of a single element. Data set is simply a list of x and y pairs, where there is only one of each x value, and y value is the count or magnitude of that value
>>> from music21 import *
>>> import random
>>> g = graph.GraphHistogram()
>>> data = [(x, random.choice(range(30))) for x in range(50)]
>>> g.setData(data)
>>> g.process()
Inherits from: Graph
Numerous horizontal bars in discrete channels, where bars can be incomplete and/or overlap. Data provided is a list of pairs, where the first value becomes the key, the second value is a list of x-start, x-length values.
>>> from music21 import *
>>> a = graph.GraphHorizontalBar(doneAction='show')
>>> data = [('Chopin', [(1810, 1849-1810)]), ('Schumanns', [(1810, 1856-1810), (1819, 1896-1819)]), ('Brahms', [(1833, 1897-1833)])]
>>> a.setData(data)
>>> a.process()
Inherits from: Graph
Grid a networkx graph – which is a graph of nodes and edges. Requires the optional networkx module.
GraphNetworxGraph attributes
- networkxGraph¶
Base class for undirected graphs. A Graph stores nodes and edges with optional data, or attributes. Graphs hold undirected edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) Python objects with optional key/value attributes. Edges are represented as links between nodes with optional key/value attributes. Parameters ———- data : input graph Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object. If the corresponding optional Python packages are installed the data can also be a NumPy matrix or 2d ndarray, a SciPy sparse matrix, or a PyGraphviz graph. name : string, optional (default=’‘) An optional name for the graph. attr : keyword arguments, optional (default= no attributes) Attributes to add to graph as key=value pairs. See Also ——– DiGraph MultiGraph MultiDiGraph Examples ——– Create an empty graph structure (a “null graph”) with no nodes and no edges.
>>> G = nx.Graph() G can be grown in several ways. **Nodes:** Add one node at a time: >>> G.add_node(1) Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph). >>> G.add_nodes_from([2,3]) >>> G.add_nodes_from(range(100,110)) >>> H=nx.path_graph(10) >>> G.add_nodes_from(H) In addition to strings and integers any hashable Python object (except None) can represent a node, e.g. a customized node object, or even another Graph. >>> G.add_node(H) **Edges:** G can also be grown by adding edges. Add one edge, >>> G.add_edge(1, 2) a list of edges, >>> G.add_edges_from([(1,2),(1,3)]) or a collection of edges, >>> G.add_edges_from(H.edges()) If some edges connect nodes not yet in the graph, the nodes are added automatically. There are no errors when adding nodes or edges that already exist. **Attributes:** Each graph, node, and edge can hold key/value attribute pairs in an associated attribute dictionary (the keys must be hashable). By default these are empty, but can be added or changed using add_edge, add_node or direct manipulation of the attribute dictionaries named graph, node and edge respectively. >>> G = nx.Graph(day="Friday") >>> G.graph {'day': 'Friday'} Add node attributes using add_node(), add_nodes_from() or G.node >>> G.add_node(1, time='5pm') >>> G.add_nodes_from([3], time='2pm') >>> G.node[1] {'time': '5pm'} >>> G.node[1]['room'] = 714 >>> G.nodes(data=True) [(1, {'room': 714, 'time': '5pm'}), (3, {'time': '2pm'})] Warning: adding a node to G.node does not add it to the graph. Add edge attributes using add_edge(), add_edges_from(), subscript notation, or G.edge. >>> G.add_edge(1, 2, weight=4.7 ) >>> G.add_edges_from([(3,4),(4,5)], color='red') >>> G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})]) >>> G[1][2]['weight'] = 4.7 >>> G.edge[1][2]['weight'] = 4 **Shortcuts:** Many common graph features allow python syntax to speed reporting. >>> 1 in G # check if node in graph True >>> print [n for n in G if n<3] # iterate through nodes [1, 2] >>> print len(G) # number of nodes in graph 5 >>> print G[1] # adjacency dict keyed by neighbor to edge attributes ... # Note: you should not change this dict manually! {2: {'color': 'blue', 'weight': 4}} The fastest way to traverse all edges of a graph is via adjacency_iter(), but the edges() method is often more convenient. >>> for n,nbrsdict in G.adjacency_iter(): ... for nbr,eattr in nbrsdict.iteritems(): ... if 'weight' in eattr: ... print (n,nbr,eattr['weight']) (1, 2, 4) (2, 1, 4) (2, 3, 8) (3, 2, 8) >>> print [ (u,v,edata['weight']) for u,v,edata in G.edges(data=True) if 'weight' in edata ] [(1, 2, 4), (2, 3, 8)] **Reporting:** Simple graph information is obtained using methods. Iterator versions of many reporting methods exist for efficiency. Methods exist for reporting nodes(), edges(), neighbors() and degree() as well as the number of nodes and edges. For details on these and other miscellaneous methods, see below.Attributes inherited from Graph: fontFamily, xTickLabelVerticalAlignment, xTickLabelHorizontalAlignment, doneAction, title, colorBackgroundFigure, colors, tickFontSize, colorGrid, figureSize, colorBackgroundData, axisKeys, grid, xTickLabelRotation, titleFontSize, alpha, labelFontSize, data, dpi, axis
GraphNetworxGraph methods
- process()¶
- No documentation.
Methods inherited from Graph: done(), setAxisLabel(), setAxisRange(), setData(), setDoneAction(), setFigureSize(), setTicks(), setTitle(), show(), write()
Inherits from: Graph
Graph two parameters in a scatter plot. Data representation is a list of points of values.
>>> from music21 import *
>>> g = graph.GraphScatter()
>>> data = [(x, x*x) for x in range(50)]
>>> g.setData(data)
>>> g.process()
Inherits from: Graph
A scatter plot where points are scaled in size to represent the number of values stored within.
>>> from music21 import *
>>> g = graph.GraphScatterWeighted()
>>> data = [(23, 15, 234), (10, 23, 12), (4, 23, 5), (15, 18, 120)]
>>> g.setData(data)
>>> g.process()
Inherits from: PlotStream
Base class for Stream plotting classes.
Plot3DBars attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
Plot3DBars properties
Properties inherited from PlotStream: id
Plot3DBars methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotMultiStream
FeatureExtractors can be ids or classes.
PlotFeatures attributes
Attributes without Documentation: format
Attributes inherited from PlotMultiStream: values
PlotFeatures methods
Methods inherited from PlotMultiStream: process(), show(), write()
Inherits from: PlotStream
Base class for Stream plotting classes.
PlotHistogram attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
PlotHistogram properties
Properties inherited from PlotStream: id
PlotHistogram methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotStream
A graph of events, sorted by pitch, over time
PlotHorizontalBar attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
PlotHorizontalBar properties
Properties inherited from PlotStream: id
PlotHorizontalBar methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Approaches to plotting and graphing multiple Streams. A base class from which Stream plotting Classes inherit.
Provide a list of Streams as an argument. Optionally provide an additional list of labels for each list. If flatten is True, the Streams will automatically be flattened.
PlotMultiStream attributes
Attributes without Documentation: format, values
PlotMultiStream methods
- process()¶
- This will process all data, as well as call the done() method. What happens when the done() is called is determined by the the keyword argument doneAction; options are ‘show’ (display immediately), ‘write’ (write the file to a supplied file path), and None (do processing but do not write or show a graph). Subclass dependent data extracted is stored in the self.data attribute.
- show()¶
- Call internal Graphs show() method independently of doneAction set and run with process()
- write(fp=None)¶
- Call internal Graphs write() method independently of doneAction set and run with process()
Inherits from: PlotStream
Base class for 2D Scatter plots.
PlotScatter attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
PlotScatter properties
Properties inherited from PlotStream: id
PlotScatter methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Inherits from: PlotStream
PlotScatterWeighted attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
PlotScatterWeighted properties
Properties inherited from PlotStream: id
PlotScatterWeighted methods
Methods inherited from PlotStream: process(), remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength(), write()
Approaches to plotting and graphing a Stream. A base class from which Stream plotting Classes inherit. This class has a number of public attributes, but these are generally not intended for direct user application. The data attribute, for example, exposes the internal data format of this plotting routine for testing, but no effort is made to make this data useful outside of the context of the Plot.
Provide a Stream as an arguement. If flatten is True, the Stream will automatically be flattened.
PlotStream attributes
Attributes without Documentation: format, values
PlotStream properties
- id¶
- Each PlotStream has a unique id that consists of its format and a string that defines the parameters that are graphed.
PlotStream methods
- process()¶
- This will process all data, as well as call the done() method. What happens when the done() is called is determined by the the keyword argument doneAction; options are ‘show’ (display immediately), ‘write’ (write the file to a supplied file path), and None (do processing but do not write or show a graph). Subclass dependent data extracted is stored in the self.data attribute.
- remapQuarterLength(x)¶
- Remap all quarter lengths.
- show()¶
- Call internal Graphs show() method independently of doneAction set and run with process()
- ticksDynamics(minNameIndex=None, maxNameIndex=None)¶
Utility method to get ticks in dynamic values.
>>> from music21 import stream; s = stream.Stream() >>> a = PlotStream(s) >>> a.ticksDynamics() [[0, '$pppppp$'], [1, '$ppppp$'], [2, '$pppp$'], [3, '$ppp$'], [4, '$pp$'], [5, '$p$'], [6, '$mp$'], [7, '$mf$'], [8, '$f$'], [9, '$fp$'], [10, '$sf$'], [11, '$ff$'], [12, '$fff$'], [13, '$ffff$'], [14, '$fffff$'], [15, '$ffffff$']] >>> a.ticksDynamics(3,6) [[3, '$ppp$'], [4, '$pp$'], [5, '$p$'], [6, '$mp$']]
- ticksOffset(offsetMin=None, offsetMax=None, offsetStepSize=None, displayMeasureNumberZero=False, minMaxOnly=False, remap=False)¶
Get offset ticks. If Measures are found, they will be used to create ticks. If not, offsetStepSize will be used to create offset ticks between min and max. The remap parameter is not yet used. If minMaxOnly is True, only the first and last values will be provided.
>>> from music21 import corpus, stream, note >>> s = corpus.parse('bach/bwv281.xml') >>> a = PlotStream(s) >>> a.ticksOffset() # on whole score, showing anacrusis spacing [[1.0, '1'], [5.0, '2'], [9.0, '3'], [13.0, '4'], [17.0, '5'], [21.0, '6'], [25.0, '7'], [29.0, '8']] >>> a = PlotStream(s.parts[0].flat) # on a Part >>> a.ticksOffset() # on whole score, showing anacrusis spacing [[1.0, '1'], [5.0, '2'], [9.0, '3'], [13.0, '4'], [17.0, '5'], [21.0, '6'], [25.0, '7'], [29.0, '8']] >>> a.ticksOffset(8, 12, 2) [[9.0, '3']] >>> a = PlotStream(s.parts[0].flat) # on a Flat collection >>> a.ticksOffset(8, 12, 2) [[9.0, '3']] >>> n = note.Note('a') # on a raw collection of notes with no measures >>> s = stream.Stream() >>> s.repeatAppend(n, 10) >>> a = PlotStream(s) # on a Part >>> a.ticksOffset() # on whole score [[0, '0'], [10, '10']]
- ticksPitchClass(pcMin=0, pcMax=11)¶
Utility method to get ticks in pitch classes
>>> from music21 import corpus >>> s = corpus.parse('bach/bwv324.xml') >>> a = PlotStream(s) >>> [x for x,y in a.ticksPitchClass()] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
- ticksPitchClassUsage(pcMin=0, pcMax=11, showEnharmonic=True, blankLabelUnused=True, hideUnused=False)¶
Get ticks and labels for pitch classes based on usage. That is, show the most commonly used enharmonic first.
>>> from music21 import corpus >>> s = corpus.parse('bach/bwv324.xml') >>> a = PlotStream(s) >>> [x for x, y in a.ticksPitchClassUsage(hideUnused=True)] [0, 2, 3, 4, 6, 7, 9, 11] >>> s = corpus.parse('bach/bwv281.xml') >>> a = PlotStream(s) >>> [x for x, y in a.ticksPitchClassUsage(showEnharmonic=True, hideUnused=True)] [0, 2, 3, 4, 5, 7, 9, 10, 11] >>> [x for x, y in a.ticksPitchClassUsage(showEnharmonic=True, blankLabelUnused=False)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> s = corpus.parse('schumann/opus41no1/movement2.xml') >>> a = PlotStream(s) >>> [x for x, y in a.ticksPitchClassUsage(showEnharmonic=True)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
- ticksPitchSpaceChromatic(pitchMin=36, pitchMax=100)¶
Utility method to get ticks in pitch space values.
>>> from music21 import stream; s = stream.Stream() >>> a = PlotStream(s) >>> [x for x,y in a.ticksPitchSpaceChromatic(60,72)] [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72]
- ticksPitchSpaceOctave(pitchMin=36, pitchMax=100)¶
Utility method to get ticks in pitch space only for every octave.
>>> from music21 import stream; s = stream.Stream() >>> a = PlotStream(s) >>> a.ticksPitchSpaceOctave() [[36, 'C2'], [48, 'C3'], [60, 'C4'], [72, 'C5'], [84, 'C6'], [96, 'C7']]
- ticksPitchSpaceUsage(pcMin=36, pcMax=72, showEnharmonic=False, blankLabelUnused=True, hideUnused=False)¶
Get ticks and labels for pitch space based on usage. That is, show the most commonly used enharmonic first.
>>> from music21 import corpus >>> s = corpus.parse('bach/bwv324.xml') >>> a = PlotStream(s.parts[0]) >>> [x for x, y in a.ticksPitchSpaceUsage(hideUnused=True)] [64, 66, 67, 69, 71, 72] >>> s = corpus.parse('schumann/opus41no1/movement2.xml') >>> a = PlotStream(s) >>> [x for x, y in a.ticksPitchSpaceUsage(showEnharmonic=True, hideUnused=True)] [36, 38, 40, 41, 43, 44, 45, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72]
- ticksQuarterLength(min=0.25, max=4, remap=True)¶
Get ticks for quarterLength. If remap is True, the remapQuarterLength() function will be used to scale displayed quarter lengths by log base 2.
>>> from music21 import stream; s = stream.Stream() >>> a = PlotStream(s)
- write(fp=None)¶
- Call internal Graphs write() method independently of doneAction set and run with process()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Aarden-Essen analysis routine. See AardenEssen for more details.
PlotWindowedAardenEssen attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedAardenEssen properties
Properties inherited from PlotStream: id
PlotWindowedAardenEssen methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotStream
Base Plot for windowed analysis routines.
PlotWindowedAnalysis attributes
Attributes without Documentation: format
Attributes inherited from PlotStream: values
PlotWindowedAnalysis properties
Properties inherited from PlotStream: id
PlotWindowedAnalysis methods
- process()¶
- Process method here overridden to provide legend.
- write(fp=None)¶
- Process method here overridden to provide legend.
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Bellman-Budge analysis routine. See BellmanBudge for more details.
PlotWindowedBellmanBudge attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedBellmanBudge properties
Properties inherited from PlotStream: id
PlotWindowedBellmanBudge methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Krumhansl-Kessler analysis routine. See KrumhanslKessler for more details.
PlotWindowedKrumhanslKessler attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedKrumhanslKessler properties
Properties inherited from PlotStream: id
PlotWindowedKrumhanslKessler methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Simple Weights analysis routine. See SimpleWeights for more details.
PlotWindowedSimpleWeights attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedSimpleWeights properties
Properties inherited from PlotStream: id
PlotWindowedSimpleWeights methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()
Inherits from: PlotWindowedAnalysis, PlotStream
Stream plotting of windowed version of Temperley-Kostka-Payne analysis routine. See TemperleyKostkaPayne for more details.
PlotWindowedTemperleyKostkaPayne attributes
Attributes without Documentation: values
Attributes inherited from PlotWindowedAnalysis: format
PlotWindowedTemperleyKostkaPayne properties
Properties inherited from PlotStream: id
PlotWindowedTemperleyKostkaPayne methods
Methods inherited from PlotWindowedAnalysis: process(), write()
Methods inherited from PlotStream: remapQuarterLength(), show(), ticksDynamics(), ticksOffset(), ticksPitchClass(), ticksPitchClassUsage(), ticksPitchSpaceChromatic(), ticksPitchSpaceOctave(), ticksPitchSpaceUsage(), ticksQuarterLength()