Pandas Dataframe Line Plot: Show Random Markers
<p>I often have dataframes with many obervations and want to have a quick glance at the data using a line plot.</p>

<p>The problem is that the colors of the colormap are either repeated over X observations or hard to distinguish e.g. in case of sequential colormaps.</p>

<p>So my idea was to add random markers to the line plot which is where I got stuck.</p>

<p>Here's an example with one markerstyle:</p>

<pre><code># -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# dataframe with random data
df = pd.DataFrame(np.random.rand(10, 8))

# plot
df.plot(kind='line', marker='d')
plt.show()
</code></pre>

<p>which delivers:</p>

<p><a href="http://i.stack.imgur.com/BpgIg.png" rel="nofollow"><img src="http://i.stack.imgur.com/BpgIg.png" alt="enter image description here"></a></p>

<p>Is it also possible to draw a (random) marker for each line?</p>

<p>Thanks in advance!</p>

<p>First we need to choose random marker. It could be done via <code>matplotlib.markers.MarkerStyle.markers</code> dictionary which contains all available markers. Also markers means 'nothing', starting with 'tick' and 'caret' should be dropped Some more <a href="http://matplotlib.org/api/markers_api.html" rel="nofollow">information</a> abour markers. Let's make list with valid markers and then random choose from them how many we need for plotting DataFrame or you could use second option with <code>filled_markers</code>:</p>

<pre><code>import matplotlib as mpl
import numpy as np

# create valid markers from mpl.markers
valid_markers = ([item[0] for item in mpl.markers.MarkerStyle.markers.items() if 
item[1] is not 'nothing' and not item[1].startswith('tick') 
and not item[1].startswith('caret')])

# use fillable markers
# valid_markers = mpl.markers.MarkerStyle.filled_markers

markers = np.random.choice(valid_markers, df.shape[1], replace=False)
</code></pre>

<p>For example:</p>

<pre><code>In [146]: list(markers )
Out[146]: ['H', '^', 'v', 's', '3', '.', '1', '_']
</code></pre>

<p>Then for markers you could plot your dataframe, and set markers for each line via <code>set_marker</code> method. Then you could add legend to your plot:</p>

<pre><code>import pandas as pd

np.random.seed(2016)
df = pd.DataFrame(np.random.rand(10, 8))

ax = df.plot(kind='line')
for i, line in enumerate(ax.get_lines()):
    line.set_marker(markers[i])

# for adding legend
ax.legend(ax.get_lines(), df.columns, loc='best')
</code></pre>

<p>Original:</p>

<p><a href="http://i.stack.imgur.com/sP8Sj.png" rel="nofollow"><img src="http://i.stack.imgur.com/sP8Sj.png" alt="enter image description here"></a></p>

<p>Modified:</p>

<p><a href="http://i.stack.imgur.com/qyA96.png" rel="nofollow"><img src="http://i.stack.imgur.com/qyA96.png" alt="enter image description here"></a></p>

python pandas matplotlib