--- title: Visualizers teaser: Visualize dependencies and entities in your browser or in a notebook version: 2 menu: - ['Dependencies', 'dep'] - ['Named Entities', 'ent'] - ['Spans', 'span'] - ['Jupyter Notebooks', 'jupyter'] - ['Rendering HTML', 'html'] - ['Web app usage', 'webapp'] --- Visualizing a dependency parse or named entities in a text is not only a fun NLP demo – it can also be incredibly helpful in speeding up development and debugging your code and training process. That's why our popular visualizers, [displaCy](https://explosion.ai/demos/displacy) and [displaCy ENT](https://explosion.ai/demos/displacy-ent) are also an official part of the core library. If you're running a [Jupyter](https://jupyter.org) notebook, displaCy will detect this and return the markup in a format [ready to be rendered and exported](#jupyter). The quickest way to visualize `Doc` is to use [`displacy.serve`](/api/top-level#displacy.serve). This will spin up a simple web server and let you view the result straight from your browser. displaCy can either take a single `Doc` or a list of `Doc` objects as its first argument. This lets you construct them however you like – using any pipeline or modifications you like. If you're using [Streamlit](https://streamlit.io), check out the [`spacy-streamlit`](https://github.com/explosion/spacy-streamlit) package that helps you integrate spaCy visualizations into your apps! ## Visualizing the dependency parse {id="dep"} The dependency visualizer, `dep`, shows part-of-speech tags and syntactic dependencies. ```python {title="Dependency example"} import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc = nlp("This is a sentence.") displacy.serve(doc, style="dep") ``` ![displaCy visualizer](/images/displacy.svg) The argument `options` lets you specify a dictionary of settings to customize the layout, for example: There's currently a known issue with the `compact` mode for sentences with short arrows and long dependency labels, that causes labels longer than the arrow to wrap. So if you come across this problem, especially when using custom labels, you'll have to increase the `distance` setting in the `options` to allow longer arcs. Moreover, you might need to modify the `offset_x` argument depending on the shape of your document. Otherwise, the left part of the document may overflow beyond the container's border. | Argument | Description | | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `compact` | "Compact mode" with square arrows that takes up less space. Defaults to `False`. ~~bool~~ | | `color` | Text color. Can be provided in any CSS legal format as a string e.g.: `"#00ff00"`, `"rgb(0, 255, 0)"`, `"hsl(120, 100%, 50%)"` and `"green"` all correspond to the color green (without transparency). Defaults to `"#000000"`. ~~str~~ | | `bg` | Background color. Can be provided in any CSS legal format as a string e.g.: `"#00ff00"`, `"rgb(0, 255, 0)"`, `"hsl(120, 100%, 50%)"` and `"green"` all correspond to the color green (without transparency). Defaults to `"#ffffff"`. ~~str~~ | | `font` | Font name or font family for all text. Defaults to `"Arial"`. ~~str~~ | | `offset_x` | Spacing on left side of the SVG in px. You might need to tweak this setting for long texts. Defaults to `50`. ~~int~~ | For a list of all available options, see the [`displacy` API documentation](/api/top-level#displacy_options). > #### Options example > > ```python > options = {"compact": True, "bg": "#09a3d5", > "color": "white", "font": "Source Sans Pro"} > displacy.serve(doc, style="dep", options=options) > ``` ![displaCy visualizer (compact mode)](/images/displacy-compact.svg) ### Visualizing long texts {id="dep-long-text",version="2.0.12"} Long texts can become difficult to read when displayed in one row, so it's often better to visualize them sentence-by-sentence instead. As of v2.0.12, `displacy` supports rendering both [`Doc`](/api/doc) and [`Span`](/api/span) objects, as well as lists of `Doc`s or `Span`s. Instead of passing the full `Doc` to `displacy.serve`, you can also pass in a list `doc.sents`. This will create one visualization for each sentence. ```python import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") text = """In ancient Rome, some neighbors live in three adjacent houses. In the center is the house of Senex, who lives there with wife Domina, son Hero, and several slaves, including head slave Hysterium and the musical's main character Pseudolus. A slave belonging to Hero, Pseudolus wishes to buy, win, or steal his freedom. One of the neighboring houses is owned by Marcus Lycus, who is a buyer and seller of beautiful women; the other belongs to the ancient Erronius, who is abroad searching for his long-lost children (stolen in infancy by pirates). One day, Senex and Domina go on a trip and leave Pseudolus in charge of Hero. Hero confides in Pseudolus that he is in love with the lovely Philia, one of the courtesans in the House of Lycus (albeit still a virgin).""" doc = nlp(text) sentence_spans = list(doc.sents) displacy.serve(sentence_spans, style="dep") ``` ## Visualizing the entity recognizer {id="ent"} The entity visualizer, `ent`, highlights named entities and their labels in a text. ```python {title="Named Entity example"} import spacy from spacy import displacy text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously." nlp = spacy.load("en_core_web_sm") doc = nlp(text) displacy.serve(doc, style="ent") ```