python - 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?
解决方案
如果您想让情绪轴从 -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
推荐阅读
- maven - Eclipse Grizzly servlet 部署程序
- java - 如何在java中的arraylist中获取每个单词的第一个和最后一个字母
- php - 如何将查询结果与其他查询结果分开
- python - Discord bot创建独特的邀请链接公会错误
- android - 错误:将 gradle 从 appcompat:1.2.0 更新为 appcompat:1.3.1 时出现“ID 未引用此 Activity 中的视图”
- json - 如何使用 JSON 数据动态创建单选按钮?
- wordpress - 发布后网站上的更改不会显示
- javascript - 带有钩子的 React 中 JSON 响应的下拉列表
- javascript - 我需要在每个页面上复制粘贴我的导航条码吗?
- ios - SwiftUI:如何将 iPad 键盘类型设置为相当于 iOS 中的 decimalPad(小)