[英]how to plot “_” objects along the X axis but varying the values on the Y axis ?(in python, matplotlib, pandas)
I have two dataframes A and B, I want to plot dataframe B values in dataframe A graph, both shared the same indices, but in point y two "_" objects will be placed one up two points and the other 2 points down the value they share in Y, how to do?我有两个数据框 A 和 B,我想在数据框 A 图中绘制数据框 B 值,它们共享相同的索引,但是在点 y 中,两个“_”对象将一个向上放置两个点,另一个向下放置 2 个点他们在Y中分享的价值,怎么办?
look like this the image=看起来像这样的图像=
dataframes: DATAFRAME A数据帧:数据帧 A
Date
2015-08-31 112.760002
2015-09-01 107.720001
2015-09-02 112.339996
2015-09-03 110.370003
2015-09-04 109.269997
2015-09-08 112.309998
2015-09-09 110.150002
2015-09-10 112.570000
2015-09-11 114.209999
2015-09-14 115.309998
DATAFRAME B
Close
2015-08-31 112.760002
2015-09-01 107.720001
2015-09-02 112.339996
2015-09-08 112.309998
Not sure to fully understand your expectation, but you should be able to manage with:不确定是否完全理解您的期望,但您应该能够管理:
import pandas as pd
import matplotlib.pyplot as plt
data_a={"date":["2015-08-31","2015-09-01", "2015-09-02","2015-09-03",
"2015-09-04","2015-09-08","2015-09-09","2015-09-10","2015-09-11","2015-09-14"],
"val":[112.760002,107.720001,112.339996,110.370003,109.269997,
112.309998,110.150002,112.570000,114.209999,115.309998]}
data_b={"date":["2015-08-31","2015-09-01","2015-09-11","2015-09-14"],
"val":[112.760002,107.720001,114.209999,115.309998]}
df_a=pd.DataFrame(data_a)
df_b= pd.DataFrame(data_b)
df_a['date']=pd.to_datetime(df_a['date'],format='%Y-%m-%d', errors='ignore')
df_b['date']=pd.to_datetime(df_b['date'],format='%Y-%m-%d', errors='ignore')
fig, ax = plt.subplots()
ax.plot(df_a['date'],df_a["val"],color='red')
ax.scatter(df_b['date'],df_b["val"]-1,marker="_",s=30,facecolor='green')
ax.scatter(df_b['date'],df_b["val"]+1,marker="_",s=30,facecolor='green')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.