如何使用 Python 或 PowerBI 中的给定数据集制作以下图表
How can I make the following charts by using the given dataset in Python or PowerBI
我有一些Python的基础知识,刚开始用Python学习绘图。
这是我的示例数据集:
Item
StartPoint
EndPoint
Type
Value
A
800
1000
1
10
A
700
815
2
20
A
850
900
2
40
A
900
990
2
30
图表应如下所示:
因此,它读取 min[start point]
和 max[end point]
来设置 ymin
和 ymax
。
以第一行为例,在y= 800
处画一条线,值为10
,在y = 1000
处画另一条线,然后填充两条线之间的空隙基于类型(不同的类型会填充不同的颜色)。
另一个例子,第二行在 y = 700
处画一条线,值为 20
,另一条线在 y = 815
,然后用不同的值填充两条线之间的空隙颜色(因为类型不同)。
这有可能吗?任何帮助都感激不尽!预先感谢大家的好意。
既然你向我保证重叠是需要的,你可能想要降低 alpha 值,这样可以更好地看到重叠。如果不需要,请删除 alpha 选项 - 在这种情况下,标准值 1 将用于绘图。如果顺序很重要,您可以在 order
列表中指定它并相应地对您的 df
进行排序,以便最后绘制此列表中的第一个条目:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"StartPoint": [800, 700, 850, 900],
"EndPoint": [1000, 815, 900, 990],
"Type": [1, 2, 3, 2],
"Value": [10, 20, 40, 30]})
color_dic = {1: "tab:red", 2: "tab:blue", 3: "tab:orange"}
order = [1, 3, 2]
df["order"] = pd.Categorical(df["Type"], order)
df = df.sort_values("order", ascending=False)
plt.bar(x=df.Value/2, height=df.EndPoint-df.StartPoint, width=df.Value, bottom=df.StartPoint, color=[color_dic[i] for i in df.Type], alpha=0.7)
plt.gca().invert_yaxis()
plt.show()
示例输出:
我有一些Python的基础知识,刚开始用Python学习绘图。
这是我的示例数据集:
Item | StartPoint | EndPoint | Type | Value |
---|---|---|---|---|
A | 800 | 1000 | 1 | 10 |
A | 700 | 815 | 2 | 20 |
A | 850 | 900 | 2 | 40 |
A | 900 | 990 | 2 | 30 |
图表应如下所示:
因此,它读取 min[start point]
和 max[end point]
来设置 ymin
和 ymax
。
以第一行为例,在y= 800
处画一条线,值为10
,在y = 1000
处画另一条线,然后填充两条线之间的空隙基于类型(不同的类型会填充不同的颜色)。
另一个例子,第二行在 y = 700
处画一条线,值为 20
,另一条线在 y = 815
,然后用不同的值填充两条线之间的空隙颜色(因为类型不同)。
这有可能吗?任何帮助都感激不尽!预先感谢大家的好意。
既然你向我保证重叠是需要的,你可能想要降低 alpha 值,这样可以更好地看到重叠。如果不需要,请删除 alpha 选项 - 在这种情况下,标准值 1 将用于绘图。如果顺序很重要,您可以在 order
列表中指定它并相应地对您的 df
进行排序,以便最后绘制此列表中的第一个条目:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"StartPoint": [800, 700, 850, 900],
"EndPoint": [1000, 815, 900, 990],
"Type": [1, 2, 3, 2],
"Value": [10, 20, 40, 30]})
color_dic = {1: "tab:red", 2: "tab:blue", 3: "tab:orange"}
order = [1, 3, 2]
df["order"] = pd.Categorical(df["Type"], order)
df = df.sort_values("order", ascending=False)
plt.bar(x=df.Value/2, height=df.EndPoint-df.StartPoint, width=df.Value, bottom=df.StartPoint, color=[color_dic[i] for i in df.Type], alpha=0.7)
plt.gca().invert_yaxis()
plt.show()
示例输出: