import plotly
import random
import plotly.graph_objects as go
from plotly.subplots import make_subplots
random.seed(42)
words = ['potato','hello','juvi']
colors = [plotly.colors.DEFAULT_PLOTLY_COLORS[random.randrange(1, 10)] for i in range(20)]
weights = [110,80,20]
data = go.Scatter(x=[random.random() for i in range(20)],
y=[random.random() for i in range(20)],
mode='text',
text=words,
marker={'opacity': 0.3},
textfont={'size': weights,
'color': colors})
layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
fig = go.Figure(data=[data], layout=layout)
fig.show()
I have the following sample code which generates a wordcloud in plotly, the thing is as you can see the words tend to get out of the given square in the plot any ideas on how I could keep the words inside the blueish square. Or at least more to the center of it
This is occurring because you're passing 20 randomly generated (x,y) coordinates to go.Scatter
, but only 3 words total, so plotly is setting the default axis ranges based on the coordinates you're passing and not the location of the 3 words.
If that was not intentional, then I would assume you meant to plot 20 randomly selected words from your list at the 20 coordinates, and you would modify your code like this:
import plotly
import random
import plotly.graph_objects as go
from plotly.subplots import make_subplots
random.seed(42)
words = ['potato','hello','juvi']
colors = [plotly.colors.DEFAULT_PLOTLY_COLORS[random.randrange(1, 10)] for i in range(20)]
weights = [110,80,20]
x = [random.random() for i in range(20)]
y = [random.random() for i in range(20)]
scatter_words = [random.choice(words) for i in range(20)]
scatter_weights = [random.choice(weights) for i in range(20)]
data = go.Scatter(x=x,
y=y,
mode='text',
text=scatter_words,
marker={'opacity': 0.3},
textfont={'size': scatter_weights,
'color': colors})
layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
fig = go.Figure(data=[data], layout=layout)
fig.show()