密谋-代表两条比例截然不同的线

时间:2018-10-20 13:46:48

标签: python python-3.x plotly sentiment-analysis

我必须展示有关社交媒体上情绪分析的作品,我需要显示加密货币的价格与其情感价值的关系。

我正在使用 plotly ,其中在html面板中在线显示该图;因此,我可以使用相同比例的数据(例如,情感与情感),但是我无法使用不同的比例来绘制情感([-1; 1])和比特币的价格 (例如[5000,70000])。

我的代码如下:

import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.tools.set_credentials_file(username='myusername', api_key='XXXXXXXXXXXX')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import datetime as dt
import numpy as np
from datetime import datetime


df_sentiment = pd.read_csv('user/my path / ......',
                           sep=None, engine='python')
df_sentiment = pd.DataFrame({'sentiment': [np.mean([int(num) for num in d[1:len(d)-1].split(',')]) for d in df_sentiment.sentiment],
                         'date': df_sentiment.date})

df_sentiment = df_sentiment.reindex(index=df_sentiment.index[::-1])
df = df_sentiment

# Create and style traces
trace0 = go.Scatter(
    x = df.date,
    y = df.sentiment,
    name = 'Bitcoin',
    line = dict(
        color = ('rgb(205, 12, 24)'),
        width = 4)
)

df_bitcoin = pd.read_csv('my path/bitcoin ...csv')
trace1 = go.Scatter(
    x = df.date,
    y = df.price,
    name = 'Bitcoin',
    mode='lines+markers',
    line = dict(
        color = ('rgb(22, 96, 167)'),
        width = 4)
)

data = [trace0, trace1]

# Edit the layout
layout = dict(title = 'Sentiment Analysis on Social Media',
              xaxis = dict(title = 'June 2018'),
              yaxis = dict(title = 'Sentiment'),
              )

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='styled-line')

我一直在使用matplotlib,并且能够表示两种类型的缩放y轴。但是我还不知道这个软件包,我在这段代码上遇到了一些困难。谢谢建议!

示例为: SENTIMENT 数据集:

0   2018-06-01  [-1, -1, 0]
1   2018-06-02  [0, 1, 0, 0, -1, -1, -1, 1, -1]
2   2018-06-03  [-1, 1, 1, 0, 0, -1, -1, -1, 0, 1, 1]
3   2018-06-04  [1, 1, 0, 1, 1, 0, 1, -1, 0]
4   2018-06-05  [1, 1, 1, -1, 1, -1]
5   2018-06-06  [1, 0, 1, -1, -1, 1, 1, 1, 1]

比特币数据集:

"Jun 30, 2018","6,391.50","6,208.20","6,518.10","6,195.80","23.86K","2.95%"
"Jun 29, 2018","6,208.10","5,848.10","6,273.00","5,782.90","33.93K","6.12%"
"Jun 28, 2018","5,850.00","6,133.10","6,167.70","5,829.90","17.94K","-4.62%"
"Jun 27, 2018","6,133.09","6,073.50","6,181.40","5,989.00","18.27K","0.90%"
"Jun 26, 2018","6,078.50","6,250.80","6,273.70","6,050.20","18.83K","-2.69%"
"Jun 25, 2018","6,246.60","6,146.10","6,334.20","6,082.10","27.91K","1.60%"

0 个答案:

没有答案