【问题标题】:How to make xticks in matplotlib evenly spaced despite their value, based on groupby?如何根据 groupby 使 matplotlib 中的 xticks 均匀分布,尽管它们具有价值?
【发布时间】:2021-03-23 19:13:21
【问题描述】:

我正在尝试绘制不同公司按年龄划分的平均回报。我希望年龄变量均匀分布,尽管步长增加并且分布不均匀。我的目标是这样的:

fig, ax = plt.subplots(figsize=(10,6), sharex=True, sharey=True)

ax.plot(ipo_cut[["IR", "Age", "Timespan"]][(ipo_cut["Timespan"] == "1980-1989") & (ipo_cut["Age"] >= 2)].groupby("Age").mean(), color="r")
ax2 = ax.twinx()
ax2.plot(ipo_cut[["IR", "Age", "Timespan"]][(ipo_cut["Timespan"] == "1990-1998") & (ipo_cut["Age"] >= 2)].groupby("Age").mean(), color = "g")
ax3 = ax.twinx()
ax3.plot(ipo_cut[["IR", "Age", "Timespan"]][(ipo_cut["Timespan"] == "1999-2000") & (ipo_cut["Age"] >= 2)].groupby("Age").mean(), color="grey")
ax4 = ax.twinx()
ax4.plot(ipo_cut[["IR", "Age", "Timespan"]][(ipo_cut["Timespan"] == "2001-2003") & (ipo_cut["Age"] >= 2)].groupby("Age").mean())

ax.set(title ="Average First-day Returns by Age of Firm at Time of IPO",
      xlabel = "Age",
      ylabel = "Average First-Day Return in %")
ax.set_ylim([0,1])
ax2.set_ylim([0,1])
ax3.set_ylim([0,1])
ax4.set_ylim([0,1])
ax.set_xlim([0,70])
ax2.set_xlim([0,70])
ax3.set_xlim([0,70])
ax4.set_xlim([0,70])
ax2.get_yaxis().set_visible(False)
ax3.get_yaxis().set_visible(False)
ax4.get_yaxis().set_visible(False)

plt.xticks([0,1,2,3,4,5,6,7,8,9,10, 11,12,13,15,17,20,30,40,50,60,70])
plt.setp(ax.get_xticklabels(), rotation = 90)

fig.show()

输出如下:

根据其他帖子,我只能显示所需的“年龄”年份,但不幸的是轴分布不均匀。我觉得我真的很接近解决方案,但我自己却找不到答案。

seaborn 中的解决方案对我也很有帮助。

使用一些附加信息进行编辑:x 向量的形状与数据不匹配。我仅针对相关年龄过滤了我的数据框,但缺少一些信息。这是一个最小的可重现示例:

import matplotlib.pyplot as py
import numpy as np
    
#create dataframe
x = np.arange(0,11)
df = pd.DataFrame(data = np.random.rand(len(x)), index = x, columns=["IR"])
df.index.name = "Age"
df.loc[[0, 9], "IR"] = np.nan
df.dropna(inplace = True)

# open figure
fig, ax = plt.subplots(figsize=(10,6))
# create x-values
x = [0,1,2,5,7,9,10]
# plot
ax.plot(x, df, color="r")
ax.set(
    title ="Average First-day Returns by Age of Firm at Time of IPO",
    xlabel = "Age",
    ylabel = "Average First-Day Return in %",
    ylim = [0,1],
    xlim = [0,10])
ax.tick_params(axis='x', labelrotation=90) 
fig.show()

我的目标是绘制没有间隙的整个数据框。所以我的想法是我在背景中有线图,x 轴上有所需的 x 值。 我希望这些信息对您有所帮助。

解决方案:

import matplotlib.pyplot as py
import numpy as np
    
#create dataframe
x = np.arange(0,10)
df = pd.DataFrame(data = np.random.rand(len(x)), index = x, columns=["IR"])
df.index.name = "Age"
#create nan in sample
df.loc[[0, 9], "IR"] = np.nan
# slice data for unbroken line (solution by max)
lg = df["IR"].isna()
# create ticks for even distribution
n = 10
a = np.arange(n)
# open figure
fig, ax = plt.subplots(figsize=(10,6))
# create x-values fir tick labels
x = (0,5,10,13,15,24,30,40,55,70)
# plot
ax.plot(a[~lg], df[~lg], color="r")
ax.set(
    title ="Average First-day Returns by Age of Firm at Time of IPO",
    xlabel = "Age",
    ylabel = "Average First-Day Return in %",
    ylim = [0,1],
    xlim = [0,10])
ax.xaxis.set_ticks(a) #set the ticks to be a
ax.xaxis.set_ticklabels(x) # change the ticks' names to x
ax.tick_params(axis='x', labelrotation=90) 
fig.show()

感谢 max 和“How to make ticks evenly spaced despite their value?”的输入,我可以找到解决问题的方法。如果您的原始数据框的 x 值的步长不是一个,我建议您只需 reset_index()。

【问题讨论】:

    标签: python matplotlib plot seaborn xticks


    【解决方案1】:

    我建议在开始时创建所需的 x 向量并在其上绘制数据。然后你可以让 matplotlib 做剩下的事情。反过来,您将人为地创建一个均匀分布的图(如果您不提供 x 值,matplotlib 假定数据是步长为 1 的点序列),您是只需更改 x 轴的外观而不是图形本身。

    import matplotlib.pyplot as py
    import numpy as np
    
    # open figure
    fig, ax = plt.subplots(figsize=(10,6))
    # create x-values
    x = [0,1,2,3,4,5,6,7,8,9,10, 11,12,13,15,17,20,30,40,50,60,70]
    # create (random) y-values
    y = np.random.rand(len(x))
    # plot
    ax.plot(x, y, color="r")
    ax.set(
        title ="Average First-day Returns by Age of Firm at Time of IPO",
        xlabel = "Age",
        ylabel = "Average First-Day Return in %",
        ylim = [0,1],
        xlim = [0,70])
    ax.tick_params(axis='x', labelrotation=90) 
    fig.show()
    

    我剥离了您的代码并创建了一些人工虚拟数据。看看:How to create a minimal reproducible example.

    【讨论】:

    • 感谢您的帮助。您的回答给了我一些想法,但不幸的是不符合我的需求。我已经编辑了我的问题以获取更多信息和一个最小的可重复示例。我希望这能让它更清楚。
    • 为了绘制一条完整的线(所以不绘制NaNs),您可以对数据进行切片lg = df["IR"].isna(),然后将其绘制为ax.plot(x[~lg], df[~lg])
    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2018-11-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多