Many examples define fig
and ax
with fig, ax = plt.subplots()
and then they directly call functions on the figure object. In contrast, other examples work directly with plt. This answer explains some of the differences, but I am still unclear about two things. First, if I do not create a separate figure object, how do I pass it to another function. For example, when I create a figure object I can pass it to mpld3
to create D3 visualization:
d3plot = mpld3.fig_to_html(fig,template_type="simple")
Second, if I do create the figure object then how do I call functions that I can call on plt on the figure? For example, I would like to able to run the following code and then pass it as a figure to mpld3 as above.
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
The other answer says that plt.subplots() unpacks a tuple. I was not sure if it would unpack what was already on the plt. So I tried:
wordcloud = WordCloud().generate(text)
plt.imshow(wordcloud)
plt.axis("off")
fig, ax = plt.subplots()
d3plot = mpld3.fig_to_html(fig,template_type="simple")
However, this just gives me a black plot, which is consistent with my prior understanding that plt.subplots()
creates entirely new figures.
Update Based on the comments, I tried the following:
wordcloud = WordCloud().generate(text)
figTopicWordCloud, ax = plt.subplots()
ax.imshow(wordcloud)
ax.axis('off')
d3plot = mpld3.fig_to_html(figTopicWordCloud, template_type="simple")
While this successfully produced the plot, it did not remove the axes from the figure.