首页 > 解决方案 > Matplotlib pyplot crush a ladder python

问题描述

I have this sample data (I report just few rows):

          date   price  sentiment
0   2018-09-18  0.0034   0.250000
1   2018-09-17  0.0034   0.083333
2   2018-09-16  0.0034   0.281250
3   2018-09-15  0.0035   0.096774
4   2018-09-14  0.0036  -0.039216
5   2018-09-13  0.0034   0.416667
6   2018-09-12  0.0031   0.061224

My code sample is this:

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

df = df.set_index(pd.to_datetime(df.date), drop=True)

params = {"text.color" : "black",
          'figure.figsize': (13, 5),
          "xtick.color" : "crimson",
          "ytick.color" : "crimson"}

plt.rcParams.update(params)
fig = plt.figure()

df.price.plot(grid=True, label="Price", legend=True, color='#228B22', title='Price vs Sentiment')
df.sentiment.plot(grid=True, secondary_y=True, label="Sentiment", legend=True, color='#3D59AB')

plt.show()

I would like to crash the sentiment line in my plot in order to take a look better at the variations, maybe showing the entire range [-1;1] of the sentiment. Any suggestions? How could I improve my plot below?

Sentiment vs Price Plot

标签: pythonpython-3.xmatplotlib

解决方案


如果您想让情绪轴从 -1 变为 1,请执行此操作

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

df = pd.DataFrame({'date': ['2018-09-18', '2018-09-17', '2018-09-16', '2018-09-15', '2018-09-14', '2018-09-13', '2018-09-12'], 
               'price': [0.0034, 0.0034, 0.0034, 0.0035, 0.0036, 0.0034, 0.0031], 
               'sentiment': [0.25, 0.083333, 0.281250, 0.096774, -0.039216, 0.416667, 0.061224]})
df = df.set_index(pd.to_datetime(df.date), drop=True)

params = {"text.color" : "black",
      'figure.figsize': (13, 5),
      "xtick.color" : "crimson",
      "ytick.color" : "crimson"}

plt.rcParams.update(params)
fig = plt.figure()
df.price.plot(grid=True, label="Price", legend=True, color='#228B22', title='Price vs Sentiment')
df.sentiment.plot(grid=True, secondary_y=True, label="Sentiment", legend=True, color='#3D59AB', ylim=(-1, 1))
plt.show()

https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.DataFrame.plot.html


推荐阅读