首页 > 解决方案 > 绘制租金图

问题描述

我必须绘制这个:

出租地块

图例应显示正在绘制租金的当前城市,x 刻度应显示月/年,y 刻度应显示该城市的租金列表

该字典包含邮政编码和城市键值对:

95136,圣何塞

95134,圣何塞

95131,圣何塞

95129,圣何塞

95128,圣何塞

95126,圣何塞

95125,圣何塞

95123,圣何塞

95117,圣何塞

95112,圣何塞

95110,圣何塞

95054,圣克拉拉

95051,圣克拉拉

95050,圣克拉拉

95035,米尔皮塔斯

95032,洛斯加托斯

95014,库比蒂诺

95008,坎贝尔

94306,帕洛阿尔托

94089,森尼韦尔

94087,森尼韦尔

94085,森尼韦尔

94043,山景

94040,山景 numpy 数组包含每个邮政编码的租金列表:

2659,2623.5,2749.5,2826.5,2775,2795,2810,2845,2827,2847,2854,2897.5,2905,2925,2902.5,2869.5

3342.5,3386,3385,3353,3300,3190,3087.5,3092,3170,3225,3340,3315,3396,3470,3480,3380

2996,2989,2953,2950,2884.5,2829,2785,2908,2850,2761,2997.5,3020,2952,2997.5,2952,2923.5

2804.5,2850.5,2850,2850,2867,2940,2905,2945,2938,2860,2884,2946,2938,2986.5,2931.5,3032.5

2800,3074,2950,2850,2850,2875,2757,2716,2738.5,2696,2809,2891,3000,2960,2950,2831

3215,3250,3290,3260,3200,3350,3507.5,3301,3276,3320,3550,3500,3530,3498.5,3505,3605

2597.5,2649,2625.5,2890,3325,3200,3010,2850,2800,2745,2695,2695,2850,2850,2860,2695

2783.5,2800,2812,2809,2813.5,2817,2815,2849,2850,2927,2810,2890,2910,2996,2950,2897

2350,2350,2350,2475,2400,2495,2567.5,2525,2400,2350,2487.5,2395,2397.5,2450,2562.5,2500

3152.5,3015,3060,3027.5,2950,3000,3002.5,3022.5,2865,2850,2825,2895,3020,3022.5,3110,3185

3337,3500,3484,3519,3393.5,3295,3062.5,3057,3123.5,3103,3079,3216,3221,3200,3214,3405.5

3180,3325,3495,3488.5,3271.5,3216,3235,3216,3300,3405,3388.5,3600,3915,3629.5,3541,3405

2800,3049,3000,3032,2995,2987.5,2850,3000,3000,2895,2992.5,3080,3378.5,3094.5,3056,3150

2871.5,2850,2751,2710,2800,2875,2845,2700,2784.5,2749,2800,2875,2905,3028,3100,3100

3552.5,3550,3540,3510,3495,3510,3512.5,3525,3300,3270,3250,3182.5,3200,3200,3250,3200

3397.5,3389,3450,3400,3300,3500,3495,3497.5,3395,3595,3350,3350,3425,3399.5,3364.5,3325.5

3550,3472,3493,3344.5,3332,3251,3270.5,3358,3370.5,3465.5,3495,3495,3500,3495,3458,3550

2805,2833,2900,2930,2795,2800,2890,2949,2800,2850,2839,2900,2850,2995,3087.5,2950.5

3495,3530,3610,4200,3990,3925,4000,3785,3792.5,3525,3495,3850,3900,4137.5,4000,4200

3545,3355,3305,3143,3220,3200,3180,3639,3260,3265,3510,3575,3695,3482.5,3600,3292.5

2995,3180,3225,3204,3219,3109,2998.5,2995,3200,3193.5,3161,3195,3200,3338.5,3200,3176

3599,3641,3796,3650,3552.5,3570,3535,3465,3400,3316,3640,3770,3440,3790,3815,3692.5

3500,3509.5,3519,3717.5,3495,3435,3285,3162.5,3425,3375,3410,3472.5,3600,3980,3657.5,3650

3773,3696,3708,3778,3689,3625,3516,3518.5,3647,3596,3685,3945,3811,3848,3699,4021.5 租金数组和字典索引应该匹配。

我有 4 个类属性 self.startMonth、self.startYear、self.endMonth 和 self.endYear。我还有一个名为 cityPlot 的函数,它接受城市、月份、年份参数,并且我在里面有一个循环,用于在字典中搜索城市,并有一个循环来查找要在 numpy 数组中搜索的邮政编码租金的索引。

def priceTrend(self, city, month, year):
    self.startMonth = month
    self.startYear = year
    count = 0
    rent = []
    cityName = ''
    for keys, cities in self.city.items():
        if self.city[keys] == city:
            cityName = self.city[cities]
            for i in self.arrRent[count]:
                rent.append(i)
                self.startMonth += 1
    plt.plot(self.startMonth, rent, lable=cityName)
    plt.legend(loc='best')
    plt.show()

我是 numpy 和绘图的新手,所以我不太确定如何进行绘图。我在绘图时遇到错误,我不确定如何修复它。

标签: pythonpython-3.xnumpymatplotlib

解决方案


首先,如果您使用 pandas 导入数据文件会更好,这样您就可以在需要时轻松地将它们转换为 numpy 数组。然后你可以这样做:

import matplotlib.pyplot as plt
%inline matplotlib
pin = ['12345','12346','12347','12348','12349','12340']
rent = [1000,2000,800,1600,900,1200]
plt.plot(pin,rent)
plt.show()

这是根据我上面考虑的数组的输出


推荐阅读