简体   繁体   English

如何沿 X 轴绘制“_”对象但改变 Y 轴上的值?(在 python、matplotlib、pandas 中)

[英]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()

Result:结果: 在此处输入图片说明

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

相关问题 如何在 Python 中使用 matplotlib plot 沿 x 轴旋转曲线? - How to plot the rotation of a curve along the x-axis with matplotlib in Python? 如何在matplotlib中的绘图上删除x,y轴上的值 - How to remove values on x,y axis on plot in matplotlib plot matplotlib 如何在 x 轴和 y 轴上颤动箭头? - Python - How to plot matplotlib quiver arrows over x-axis and y-axis? - Python 如何在大熊猫的X轴上绘制Y轴和月份的年份? - How to make a years on Y axis and months on X axis plot with pandas? 如图所示,x 轴为 plot 天,y 轴为时间,热图 plot 为 python 中的值? - How to plot day in x axis, time in y axis and a heatmap plot for the values in python as shown in the figure? 在 matplotlib 中制作散点图,x 轴上有日期,y 上有值 - Make a Scatter Plot in matplotlib with dates on x axis and values on y Python Matplotlib - 如何在y轴上指定值? - Python Matplotlib - how to specify values on y axis? 如何在 Matplotlib 中沿 x 轴对 plot 分组列? - How to plot grouped columns along the x-axis in Matplotlib? Matplotlib:如何 plot 两个条形图具有相同的 x/y 轴,但一个沿 y 轴从另一个开始 - Matplotlib : How to plot two bar plots with the same x/y axes but one start over the other along y axis matplotlib 多 Y 轴熊猫图 - matplotlib multiple Y-axis pandas plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM