简体   繁体   English

Python plot 数据为条形蜡烛图

[英]Python plot data as bar-candle-like chart

I have a dataset with following structue:我有一个具有以下结构的数据集:

[Title,Value_A,Value_B]

Some real world examples are:一些现实世界的例子是:

[A,0.1,0.3]
[B,0.5,0.6]
[I,1,0.6]
[R,0.6,0.7]

I want a bar-like chart like this sketch:我想要一个像这样的草图的条形图:

Example Image示例图像

I want to use matplotlib.pyplot lib, tried a lot but just need a hint.我想使用 matplotlib.pyplot lib,尝试了很多但只需要提示。 Also,i am open for other libraries.另外,我对其他图书馆开放。

Edit1: Sorry i was not precise enough. Edit1:对不起,我不够精确。 Data is stored in Pandas DataFrame数据存储在 Pandas DataFrame

import matplotlib.pyplot as plt
import pandas as pd

df=pd.DataFrame()
df["title"] = ("A","B","C","D")
df["value_1"] = (0.1,0.5,1,0.6)
df["value_2"] = (0.3,0.6,0.6,0.7)
df.plot(x="title", y="value_1", kind="bar",stacked=True)
plt.show()

This is what i got as simple start.这就是我得到的简单开始。 My Problem is that i dont know how to feed the y axes with 2 values which are present as a bar/candle.我的问题是我不知道如何为 y 轴提供 2 个值,这些值作为条形/蜡烛存在。

Maybe this one is what you need:也许这就是你需要的:

import matplotlib.pyplot as plt

collectn_1 = [0.1,0.3]
collectn_2 = [0.5,0.6]
collectn_3 = [0.6,1]
collectn_4 = [0.6,0.7]

## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]

# Create a figure instance
fig = plt.figure()

# Create an axes instance
ax = fig.add_axes([0,0,1,1])

# Create the boxplot
bp = ax.violinplot(data_to_plot)
plt.show()

You can use plt.bar :您可以使用plt.bar

fig, ax = plt.subplots()

ax.bar(x=df['title'], 
       bottom=df['value_1'], 
       height=df['value_2'] - df['value_1'])

ax.set_ylim(bottom=0, top=1.2)
new_yticks = list(set(df['value_1']).union(df['value_2']))
plt.yticks(new_yticks)
plt.show()

绘图结果


Alternatively as @LuisAlejandro suggested, you can use ax.violinplot , but you have to extract the values from the dataframe first:或者,正如@LuisAlejandro 建议的那样,您可以使用ax.violinplot ,但您必须先从 dataframe 中提取值:

vals = list(df[['value_1', 'value_2']].T.to_dict('list').values())
# [[0.1, 0.3], [0.5, 0.6], [1.0, 0.6], [0.6, 0.7]]

fig, ax = plt.subplots()

ax.violinplot(vals)

ax.set_ylim(bottom=0, top=1.2)
plt.yticks(new_yticks)
plt.show()

小提琴情节

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM