I am trying to replace
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)
with the for loop code
totalPlot = 2
x = ['trace%s'%(item + 1) for item in range(totalPlot)]
y = [(item + 1) for item in range(totalPlot)]
z = totalPlot * [1]
for i, j, k in zip(x, y, z):
fig.append_trace(i, j, k)
so that it will be more scalable as I add more figures. However, the loop code keeps returning 'TypeError: 'str' object does not support item assignment'. What did I do wrong?
Here is the code similar with the one from Plotly:
from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Scatter(
x=[1, 2, 3],
y=[4, 5, 6]
)
trace2 = go.Scatter(
x=[20, 30, 40],
y=[50, 60, 70],
)
fig = tools.make_subplots(rows=2, cols=1)
# ----- Loop here ------- START
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)
# ----- Loop here ------- END
fig['layout'].update(height=600, width=600, title='i <3 subplots')
py.iplot(fig, filename='make-subplots')
'TypeError: 'str' object does not support item assignment'tends to arise when you try to modify a string object in place (change a character for example.) Strings are immutable, so you can't "change" one but you can make a changed copy.