设置plt.ylim

时间:2018-12-05 13:46:13

标签: python matplotlib

我正在尝试设置一些我正在研究的极坐标图的ylim。问题是,当我在函数内部更改plt.ylim时,连接极坐标图内部形状的填充和线条断开。我不知道为什么会这样。我已经附上了一个截图,显示在下面。

enter image description here

有人对如何解决此问题有任何建议吗?我在下面插入了函数和示例数据框。

import matplotlib.pyplot as plt
import pandas as pd    

def make_spider(row, title, color):

    import math

    categories = list(df)
    N = len(categories)

    angles = [n / float(N) * 2 * math.pi for n in range(N)]
    angles += angles[:1]

    ax = plt.subplot(1, 5, row+1, polar=True)

    plt.xticks(angles[:-1], categories, color='grey', size=8)

    values = df.iloc[row].values.flatten().tolist()
    values += values[:1]

    ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
    ax.fill(angles, values, color=color, alpha = .4)

    # here is the problematic line of code
    plt.ylim(-.3, .4)

my_dpi = 40

plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=96)

my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)

for row in range(0, len(df.index)):
     make_spider( row  = row, title='Cluster: ' + str(row), color=my_palette(row) )

示例数据框:

df = pd.DataFrame.from_dict({"no_rooms":{"0":-0.3470532925,"1":-0.082144001,"2":-0.082144001,"3":-0.3470532925,"4":-0.3470532925},"total_area":{"0":-0.1858487321,"1":-0.1685491141,"2":-0.1632483955,"3":-0.1769700284,"4":-0.0389887094},"car_park_spaces":{"0":-0.073703681,"1":-0.073703681,"2":-0.073703681,"3":-0.073703681,"4":-0.073703681},"house_price":{"0":-0.2416123064,"1":-0.2841806825,"2":-0.259622004,"3":-0.3529449824,"4":-0.3414842657},"pop_density":{"0":-0.1271390651,"1":-0.3105853643,"2":-0.2316607937,"3":-0.3297832328,"4":-0.4599021194},"business_rate":{"0":-0.1662745006,"1":-0.1426329043,"2":-0.1577528867,"3":-0.163560133,"4":-0.1099718326},"noqual_pc":{"0":-0.0251535462,"1":-0.1540641646,"2":-0.0204666924,"3":-0.0515740013,"4":-0.0445135996},"level4qual_pc":{"0":-0.0826103951,"1":-0.1777759951,"2":-0.114263357,"3":-0.1787044751,"4":-0.2709496389},"badhealth_pc":{"0":-0.105481688,"1":-0.1760349683,"2":-0.128215043,"3":-0.1560577648,"4":-0.1760349683}})

2 个答案:

答案 0 :(得分:2)

这里的问题是,当线超出轴限制时,线会被“吞噬”。设置plt.ylim(-.3, .4)时,最后两个图表中有一些点不在此范围内,即在-0.3之下。为了显示它们,您需要设置限制以包括这些点。例如。

plt.ylim(-.5, .4)

enter image description here

在下面,我还将径向网格设置为间隔0.2,以使该图看起来不太拥挤。完整的可运行示例:

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


def make_spider(df, row, title, color):
    categories = list(df)
    N = len(categories)

    angles = [n / float(N) * 2 * np.pi for n in range(N)]
    angles += angles[:1]

    ax = plt.subplot(1, 5, row+1, polar=True)

    plt.xticks(angles[:-1], categories, color='grey', size=8)
    plt.yticks(np.arange(-.4,.5,.2))
    plt.ylim(-.5, .4)

    values = df.iloc[row].values.flatten().tolist()
    values += values[:1]

    ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
    ax.fill(angles, values, color=color, alpha = .4)



df = pd.DataFrame.from_dict(
        {"no_rooms":{"0":-0.3470532925,"1":-0.082144001,"2":-0.082144001,
                     "3":-0.3470532925,"4":-0.3470532925},
        "total_area":{"0":-0.1858487321,"1":-0.1685491141,"2":-0.1632483955,
                      "3":-0.1769700284,"4":-0.0389887094},
        "car_park_spaces":{"0":-0.073703681,"1":-0.073703681,"2":-0.073703681,
                           "3":-0.073703681,"4":-0.073703681},
        "house_price":{"0":-0.2416123064,"1":-0.2841806825,"2":-0.259622004,
                       "3":-0.3529449824,"4":-0.3414842657},
        "pop_density":{"0":-0.1271390651,"1":-0.3105853643,"2":-0.2316607937,
                       "3":-0.3297832328,"4":-0.4599021194},
        "business_rate":{"0":-0.1662745006,"1":-0.1426329043,"2":-0.1577528867,
                         "3":-0.163560133,"4":-0.1099718326},
        "noqual_pc":{"0":-0.0251535462,"1":-0.1540641646,"2":-0.0204666924,
                     "3":-0.0515740013,"4":-0.0445135996},
        "level4qual_pc":{"0":-0.0826103951,"1":-0.1777759951,"2":-0.114263357,
                         "3":-0.1787044751,"4":-0.2709496389},
        "badhealth_pc":{"0":-0.105481688,"1":-0.1760349683,"2":-0.128215043,
                        "3":-0.1560577648,"4":-0.1760349683}})

my_dpi = 40

plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=96)

my_palette = plt.cm.get_cmap('Set2', len(df.index)+1)

for row in range(0, len(df.index)):
     make_spider(df, row  = row, title='Cluster: ' + str(row), color=my_palette(row) )

plt.show()

答案 1 :(得分:1)

极轴具有set_rmax()功能see here。将plt.ylim()行更改为

plt.gca().set_rmax(.4)

解决了问题。