Black Lives Matter. Please consider donating to Black Girls Code today.

Getting different markers shapes

This is my first post and is similar to this one (Different marker shapes in 3d scatter plot), but I can’t seem to get anything to work in python. First off, I am trying to make a multi-line graph (which I have done in both plt and plotly…code below), but being colorblind (which I am) I can’t often tell what I am looking in plotly because the markers are always a circle (even though the label is included, it sometimes gets cut off and I can’t figure out what I’m looking at).

I am using Jupyter Notebook ##(version: 5.4.0)
Python ##(version 3.6.4)

Screenshot of the dummy_data file.

In matplotlib, I did the following to get the output attached (note the different shape markers):

import matplotlib as mpl ##(version: 2.1.2)
import pandas as pd ##(version: 0.22.0)
import numpy as np ##(version: 1.14.0)

import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import cufflinks as cf ##(version: 0.12.1)

init_notebook_mode(connected=True)
cf.go_offline()

%matplotlib notebook

fx = df.groupby(['studyarm', 'visit'])['totdiffic_chg'].mean().unstack('studyarm').drop(['02_UNSCH','ZEOS'])


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')])

markers = np.random.choice(valid_markers, df.shape[1], replace=False)
ax = fx.plot(kind = 'line', linestyle='-')
for i, line in enumerate(ax.get_lines()):
    line.set_marker(markers[i])
ax.legend(loc='best')
ax.set_xticklabels(df.index, rotation=45)
plt.title('Some Made Up Data')
plt.ylabel('Score', fontsize=14)
plt.autoscale(enable=True, axis='x', tight=True)
plt.tight_layout()

I used the code below and it created the graph via plotly/cufflinks:

fx.iplot(kind='line', yTitle='Score', title='Some Made Up Data', mode=markers,\
           filename='cufflinks/simple-line')

I have searched the web for the last few days and I can see many options to change the color, opacity, etc., etc., but I can’t seem to figure out a way to automatically and randomly change the shape of the markers OR to manually change each individual line to a separate marker shape.

I am sure this is a simple fix, but I can’t figure it out. Any help (or nudge in the right direction) would be very much appreciated.!

First, you need to have cufflinks return the plot as a plotly figure:

fig = fx.iplot(kind='line', yTitle='Score', title='Some Made Up Data', mode=markers, asFigure=True)

Then, you need to set the marker symbol for each trace. Available marker symbols are listed here (all of the ones in your matplotlib example are available): https://plot.ly/python/reference/#scatter-marker-symbol

To set the symbol of the first trace, for example:

fig['data'][0]['marker']['symbol'] = 'triangle-left'
2 Likes

Hi Jack,

Thank You for your response. It works! Now I will start to mess with the code to autogenerate these.