简体   繁体   English

无法使用 plotly 在 python 中显示图

[英]not able to display plots in python with plotly

Hello I am basically new to python... I am running the code to find out no of frequencies of the words and plots in the basis of bar plot.你好,我基本上是 python 的新手......我正在运行代码以找出基于条形图的单词和图的频率。

Follow is the code:以下是代码:

lyrics="Ah, Ba Ba Ba Ba Barbara Ann Ba Ba Ba Ba Barbara Ann Oh Barbara Ann Take My Hand Barbara Ann You Got Me Rockin' And A-Rollin' Rockin' And A-Reelin' Barbara Ann Ba Ba Ba Barbara Ann ...More Lyrics... Ba Ba Ba Ba Barbara Ann Ba Ba Ba Ba Barbara Ann"
list_of_lyrics = lyrics.split(' ')
len(list_of_lyrics)
unique_words = set(list_of_lyrics)
len(unique_words)
word_histogram = dict.fromkeys(unique_words, 0)
for word in list_of_lyrics:
    word_histogram[word] = word_histogram[word]+ 1

import plotly
from plotly.offline import iplot, init_notebook_mode
from plotly import tools
import plotly.graph_objs as go
init_notebook_mode(connected=True)

trace = {'type': 'bar', 'x': list(unique_words), 'y': list(word_histogram.values())}

plotly.offline.iplot({'data': [trace]})

output is:输出是:

{'text/html': "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>", 'text/vnd.plotly.v1+html': "<script>requirejs.config({paths: { 'plotly': }

Worked solution for Python3:适用于 Python3 的解决方案:

#import all the neccessaries packages
import plotly
import plotly.graph_objs as go
#Your code almost without changes
lyrics="Ah, Ba Ba Ba Ba Barbara Ann Ba Ba Ba Ba Barbara Ann Oh Barbara Ann " \
       "Take My Hand Barbara Ann You Got Me Rockin' And A-Rollin' Rockin' " \
       "And A-Reelin' Barbara Ann Ba Ba Ba Barbara Ann ...More Lyrics... " \
       "Ba Ba Ba Ba Barbara Ann Ba Ba Ba Ba Barbara Ann" 
list_of_lyrics = lyrics.split(" ") 
print(len(list_of_lyrics)) 
unique_words = set(list_of_lyrics) 
print(len(unique_words)) 
word_histogram = dict.fromkeys(unique_words, 0) 
for word in list_of_lyrics:
    word_histogram[word] = word_histogram[word]+ 1
print(word_histogram)
# Create trace (needed to do a plot)
trace = go.Bar(x = list(unique_words), y = list(word_histogram.values()))
# Set our trace in list which named data
data = [trace]
# Specify layout if you want to add a titles to your plot
layout = go.Layout(title = "Lyrics frequency",
                   xaxis = dict(title="Words from lyrics list"),
                   yaxis = dict(title="Frequency of each word in the list")
                   )
# Create fig, that contains all what we need to do a plot
fig = go.Figure(data=data, layout=layout)
# Call plotly in offline mode, choose name for plot and save him in directory
# where your Python script is
plotly.offline.plot(fig, filename="Lyrics frequency.html", auto_open=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM