Plotly¤
The plotly
submodule contains methods of producing visualizations using the Plotly graphing library.
Important
There is currently a problem displaying Plotly plots in Jupyter notebooks (see issue #23). The workaround is to save the plot and open it in a different browser window.
The plotly
submodule contains components for generating word clouds and cluster analysis plots.
Plotly Word Clouds¤
lexos.visualization.plotly.cloud.wordcloud.make_wordcloud(data, opts=None, round=None)
¤
Make a word cloud.
Accepts data from a string, list of lists or tuples, a dict with terms as keys and counts/frequencies as values, or a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[dict, list, object, str, tuple]
|
The data. Accepts a text string, a list of lists or tuples, a dict with the terms as keys and the counts/frequencies as values, or a dataframe with "term" and "count" or "frequency" columns. |
required |
opts |
dict
|
The WordCloud() options. For testing, try {"background_color": "white", "max_words": 2000, "contour_width": 3, "contour_width": "steelblue"} |
None
|
round |
int
|
An integer (generally between 100-300) to apply a mask that rounds the word cloud. |
None
|
Returns:
Type | Description |
---|---|
object
|
word cloud (object): A WordCloud object if show is set to False. |
Notes
- For a full list of options, see https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud-wordcloud.
- If
show=False
the function expects to be called with something likewordcloud = make_wordcloud(data, show=False)
. This returns WordCloud object which can be manipulated by any of its methods, such asto_file()
. See the WordCloud documentation for a list of methods.
Source code in lexos\visualization\plotly\cloud\wordcloud.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
lexos.visualization.plotly.cloud.wordcloud.plot(dtm, docs=None, opts=None, layout=None, show=True)
¤
Convert a Python word cloud to a Plotly word cloud.
This is some prototype code for generating word clouds in Plotly. Based on https://github.com/PrashantSaikia/Wordcloud-in-Plotly.
This is really a case study because Plotly does not do good
word clouds. One of the limitations is that WordCloud.layout_
always returns None
for orientation and frequencies for
counts. That limits the options for replicating its output.
Run with:
from lexos.visualization.plotly.cloud.wordcloud import plot
plot(dtm)
or
wc = plot(dtm, show=False)
wc.show()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtm |
object
|
A lexos.DTM object. |
required |
docs |
List[str]
|
(List[str]): A list of document names to use. |
None
|
opts |
dict
|
(dict): A dict of options to pass to WordCloud. |
None
|
layout |
dict
|
(dict): A dict of options to pass to Plotly. |
None
|
show |
bool
|
(bool): Whether to show the plot. |
True
|
Returns:
Name | Type | Description |
---|---|---|
object |
go.Figure
|
A Plotly figure. |
Source code in lexos\visualization\plotly\cloud\wordcloud.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
Plotly Clustermaps¤
lexos.visualization.plotly.cluster.clustermap.PlotlyClustermap
¤
PlotlyClustermap.
Source code in lexos\visualization\plotly\cluster\clustermap.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
__init__(dtm, metric='euclidean', method='average', hide_upper=False, hide_side=False, colorscale='Viridis', width=600, height=600, title=None, config=dict(displaylogo=False, modeBarButtonsToRemove=['toImage', 'toggleSpikelines'], scrollZoom=True), show=False)
¤
Initialise the Clustermap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtm |
Any
|
The document-term-matrix |
required |
Source code in lexos\visualization\plotly\cluster\clustermap.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
build()
¤
Build a clustermap.
Source code in lexos\visualization\plotly\cluster\clustermap.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
savefig(filename)
¤
Save the figure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The name of the file to save. |
required |
Source code in lexos\visualization\plotly\cluster\clustermap.py
231 232 233 234 235 236 237 |
|
showfig()
¤
Show the figure.
Source code in lexos\visualization\plotly\cluster\clustermap.py
239 240 241 |
|
to_html(show_link=False, output_type='div', include_plotlyjs=False, filename=None, auto_open=False, config=None)
¤
Convert the figure to HTML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_link |
bool
|
For exporting to Plotly cloud services. Default is |
False
|
output_type |
str
|
If that contains the graph and the script to
generate the graph. Use file if you want to save and view a single
graph at a time in a standalone HTML file. Use div if you are embedding
these graphs in an existing HTML file. Default is div . |
'div'
|
include_plotlyjs |
bool
|
If True, include the plotly.js source code in the
output file or string, which is good for standalone web pages but makes
for very large files. If you are embedding the graph in a webpage, it
is better to import the plotly.js library and use a |
False
|
filename |
str
|
The local filename to save the outputted chart to. If the
filename already exists, it will be overwritten. This argument only applies
if output_type is |
None
|
auto_open |
bool
|
If True, open the saved file in a web browser after saving.
This argument only applies if output_type is |
False
|
config |
dict
|
A dict of parameters in the object's configuration. |
None
|
Note
This method uses plotly.offline.plot
, which no longer appears to be documented.
It has been replaced by renderers: https://plotly.com/python/renderers/. However,
there does not appear to be an HTML renderer, so no attempt has been made to
use the new functionality.
Source code in lexos\visualization\plotly\cluster\clustermap.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
lexos.visualization.plotly.cluster.dendrogram.PlotlyDendrogram
¤
PlotlyDendrogram.
Typical usage:
from lexos.visualization.plotly.cluster.dendrogram import PlotlyDendrogram
dendrogram = PlotlyDendrogram(dtm, show=True)
or
dendrogram = PlotlyDendrogram(dtm)
dendrogram.fig
Needs some work in returning the figure as a figure
and html and html div.
Source code in lexos\visualization\plotly\cluster\dendrogram.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
__init__(dtm, labels=None, metric='euclidean', method='average', truncate_mode=None, get_leaves=True, orientation='bottom', title=None, figsize=(10, 10), show=False, colorscale=None, hovertext=None, color_threshold=None, config=dict(displaylogo=False, modeBarButtonsToRemove=['toImage', 'toggleSpikelines'], scrollZoom=True), x_tickangle=0, y_tickangle=0, **layout)
¤
Initialise the Dendrogram.
Source code in lexos\visualization\plotly\cluster\dendrogram.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
build()
¤
Build a dendrogram.
Source code in lexos\visualization\plotly\cluster\dendrogram.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
showfig()
¤
Show the figure.
Calling Dendrogram.fig
when the dendrogram has been set
to False
does not apply the config (there is no way to
do this in Plotly. Calling Dendrogram.showfig()
rebuilds
the fig with the config applied.
Source code in lexos\visualization\plotly\cluster\dendrogram.py
168 169 170 171 172 173 174 175 176 177 |
|
to_html(show_link=False, output_type='div', include_plotlyjs=False, filename=None, auto_open=False, config=None)
¤
Convert the figure to HTML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_link |
bool
|
For exporting to Plotly cloud services. Default is |
False
|
output_type |
str
|
If that contains the graph and the script to
generate the graph. Use file if you want to save and view a single
graph at a time in a standalone HTML file. Use div if you are embedding
these graphs in an existing HTML file. Default is div . |
'div'
|
include_plotlyjs |
bool
|
If True, include the plotly.js source code in the
output file or string, which is good for standalone web pages but makes
for very large files. If you are embedding the graph in a webpage, it
is better to import the plotly.js library and use a |
False
|
filename |
str
|
The local filename to save the outputted chart to. If the
filename already exists, it will be overwritten. This argument only applies
if output_type is |
None
|
auto_open |
bool
|
If True, open the saved file in a web browser after saving.
This argument only applies if output_type is |
False
|
config |
dict
|
A dict of parameters in the object's configuration. |
None
|
Note
This method uses plotly.offline.plot
, which no longer appears to be documented.
It has been replaced by renderers: https://plotly.com/python/renderers/. However,
there does not appear to be an HTML renderer, so no attempt has been made to
use the new functionality.
Source code in lexos\visualization\plotly\cluster\dendrogram.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
lexos.visualization.plotly.cluster.dendrogram._get_dummy_scatter(x_value)
¤
Create a invisible scatter point at (x_value, 0).
Use this function to help extend the margin of the dendrogram plot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_value |
float
|
The desired x value we want to extend the margin to. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
Scatter
|
An invisible scatter point at (x_value, 0). |
Source code in lexos\visualization\plotly\cluster\dendrogram.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|