在Matplotlib中将柱形图垂直堆叠打印。

huangapple go评论89阅读模式
英文:

Print bars one above another in matplotlib

问题

I have tried following script ...

import matplotlib.pyplot as plot
import pandas as pd

data = {"PEOI": [100,129,203,406,92,69,69,51,55,34,371], "PCHOI":[-3,-3,-4,10,37,2,-6,16,7,2,-44], "STKS":[4800,4810,4820,4840,4850,4860,4880,4900,4910,4920,4940], "CCHOI":[50,74,45,617,54,2,35,3,3,10,13], "CEOI":[177,182,67,562,118,135,112,30,32,15,184]}
df = pd.DataFrame(data);

ax1 = df.plot.bar(x="STKS", y=["CEOI","PEOI"], rot=90, width = 0.9, title ="OPEN INTEREST BUILDUP", figsize=(10, 6), fontsize=20)
ax1.set_xlabel("STRIKES", fontweight ='bold', fontsize=25)

ax1.set_ylabel("CHANGE IN OI", fontweight ='bold', fontsize=25)
plot.show(block=True);

Now I have 2 queries...

1- How to make "CEOI" & "PEOI" bars to print one above another (overlapping completely) with similar gridline as in below image 在Matplotlib中将柱形图垂直堆叠打印。

2- I need two plots at the same time (subplot) for the dataset from my DataFrame....
(a) PEOI, CEOI, STKS
(b) PCHOI, CCHOI, STKS

英文:

I have tried following script ...

import matplotlib.pyplot as plot
import pandas as pd

data = {"PEOI": [100,129,203,406,92,69,69,51,55,34,371], "PCHOI":[-3,-3,-4,10,37,2,-6,16,7,2,-44], "STKS":[4800,4810,4820,4840,4850,4860,4880,4900,4910,4920,4940], "CCHOI":[50,74,45,617,54,2,35,3,3,10,13], "CEOI":[177,182,67,562,118,135,112,30,32,15,184]}     
df = pd.DataFrame(data);

ax1 = df.plot.bar(x="STKS", y=["CEOI","PEOI"], rot=90, width = 0.9, title ="OPEN INTEREST BUILDUP", figsize=(10, 6), fontsize=20)
ax1.set_xlabel("STRIKES", fontweight ='bold', fontsize=25)

ax1.set_ylabel("CHANGE IN OI", fontweight ='bold', fontsize=25)
plot.show(block=True);

Now I have 2 queries...

1- How to make "CEOI"&"PEOI" bars to print one above another(overlapping completely) withe similar gridline as in below image
在Matplotlib中将柱形图垂直堆叠打印。

2- I need two plots at the same time(subplot) for the dataset from my DataFrame....
(a) PEOI, CEOI, STKS
(b) PCHOI, CCHOI, STKS

答案1

得分: 0

我不认为你可以使用pandas来实现你想要的,所以我们将不得不使用matplotlib从头开始构建这个图表。对于柱状图重叠在彼此前面,只需创建两个柱状图,给它们相同的x值,并以不同的宽度绘制它们。对于子图,我创建了一个列表,用于确定哪些数据属于哪个图,并循环遍历它们,以及相应的坐标轴,以创建这些图。代码的其余部分用于格式设置(字体大小和旋转,标签等)。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = {"PEOI": [100, 129, 203, 406, 92, 69, 69, 51, 55, 34, 371],
        "PCHOI": [-3, -3, -4, 10, 37, 2, -6, 16, 7, 2, -44],
        "STKS": [4800, 4810, 4820, 4840, 4850, 4860, 4880, 4900, 4910, 4920, 4940],
        "CCHOI": [50, 74, 45, 617, 54, 2, 35, 3, 3, 10, 13],
        "CEOI": [177, 182, 67, 562, 118, 135, 112, 30, 32, 15, 184]}
df = pd.DataFrame(data)

min_diff_x = np.diff(df.STKS).min()
w1 = 0.8*min_diff_x
w2 = 0.5*min_diff_x

fig, axes = plt.subplots(2, 1, figsize=(10,15))
for (y1, y2), ax in zip([("PEOI", "CEOI"), ("PCHOI", "CCHOI")], axes):
    ax.bar(df["STKS"], df[y1], width=w1, label=y1)
    ax.bar(df["STKS"], df[y2], width=w2, label=y2)
    ax.set_xlabel("STRIKES", fontweight="bold", fontsize=25)
    ax.set_ylabel("CHANGE IN OI", fontweight="bold", fontsize=25)
    ax.tick_params(labelsize=20)
    ax.tick_params(axis="x", rotation=90)
    ax.legend()
    ax.grid()
fig.suptitle("OPEN INTEREST BUILDUP", fontsize=20)
fig.tight_layout()
英文:

I don't think you can do what you want using pandas, so we will have to use matplotlib to build this plot from scratch. For the bars being in front of each other, it's just a matter of making two bar plots, giving them the same x values, and plotting them with different widths. For the subplots, I created a list for which data goes to which plot and looped through them, along with the axes, to create the plots. The rest of the code is for formatting (font size and rotation, labels, etc.).

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = {"PEOI": [100, 129, 203, 406, 92, 69, 69, 51, 55, 34, 371],
        "PCHOI": [-3, -3, -4, 10, 37, 2, -6, 16, 7, 2, -44],
        "STKS": [4800, 4810, 4820, 4840, 4850, 4860, 4880, 4900, 4910, 4920, 4940],
        "CCHOI": [50, 74, 45, 617, 54, 2, 35, 3, 3, 10, 13],
        "CEOI": [177, 182, 67, 562, 118, 135, 112, 30, 32, 15, 184]}
df = pd.DataFrame(data)

min_diff_x = np.diff(df.STKS).min()
w1 = 0.8*min_diff_x
w2 = 0.5*min_diff_x

fig, axes = plt.subplots(2, 1, figsize=(10,15))
for (y1, y2), ax in zip([("PEOI", "CEOI"), ("PCHOI", "CCHOI")], axes):
    ax.bar(df["STKS"], df[y1], width=w1, label=y1)
    ax.bar(df["STKS"], df[y2], width=w2, label=y2)
    ax.set_xlabel("STRIKES", fontweight="bold", fontsize=25)
    ax.set_ylabel("CHANGE IN OI", fontweight="bold", fontsize=25)
    ax.tick_params(labelsize=20)
    ax.tick_params(axis="x", rotation=90)
    ax.legend()
    ax.grid()
fig.suptitle("OPEN INTEREST BUILDUP", fontsize=20)
fig.tight_layout()

在Matplotlib中将柱形图垂直堆叠打印。

huangapple
  • 本文由 发表于 2023年7月28日 00:00:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定