4

This post is similar to this one (Change Marker Shapes in Plotly .js), 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 (i.e., when the labels are too long) and I can't figure out what I'm looking at). The plotly/cufflinks graphs are much better in terms of being interactive and since I do a lot of data presentations, this will be my preferred method going forward if I can figure out how to change the markers for each line.

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

Screenshot of the dummy_data file.

dummy_data_screenshot

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

import matplotlib.pyplot as plt
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

df = pd.read_csv("desktop\dummy_data.csv")

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()

plt_image_dummy_data

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

plotly_image_dummy_data

I have searched the web for the last few days and I can see many options to change the marker 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.!

t3c
  • 109
  • 1
  • 7

1 Answers1

4

You can specify the shape for scatter plots using the symbol property, like bellow:

 Scatter(x = ..., y = ..., mode = 'lines+markers',
         marker = dict(size = 10, symbol = 1, ...))

For example:

  • 0 gives circles
  • 1 gives squares
  • 3 gives '+' signs
  • 5 gives triangles, etc.

Have a look at the 'symbol' entry in Plotly's doc here: https://plot.ly/python/reference/#box-marker-symbol

byouness
  • 1,746
  • 2
  • 24
  • 41