叠加条形图,循环需要太长时间

时间:2015-10-09 19:50:40

标签: python matplotlib

这是我的代码:

import time
import numpy as np
import matplotlib.pyplot as plt



fig, ax = plt.subplots()

list_date = ['2015', '2016', '2017']

data_dict_A = {'2015': {1: 254, 2:567, 3:786}, 
             '2016':{1:567, 2:189, 3:145},
             '2017': {1:89, 2:123, 3:190}}
data_dict_B = {'2015': {1: 154, 2:597, 3:286}, 
             '2016':{1:967, 2:789, 3:345},
             '2017': {1:189, 2:223, 3:290}}
data_dict_C = {'2015': {1: 354, 2:591, 3:486}, 
             '2016':{1:267, 2:289, 3:367},
             '2017': {1:489, 2:256, 3:390}}

length = np.arange(1, 4, 1)
width = 0.2

for each_date in list_date:
    for d_A, v_A in data_dict_A.items():
        for d_B, v_B in data_dict_B.items():
            for d_C, v_C in data_dict_C.items():
                if each_date in data_dict_A.keys():
                    graph_A = plt.bar(length-0.2, v_A.values(), width, color="blue", align='center')
                    graph_B = plt.bar(length, v_B.values(), width, color="red", align='center')
                    graph_C = plt.bar(length+0.2, v_C.values(), width, color="yellow", align='center')
                    plt.legend((graph_A[0], graph_B[0], graph_C[0]), ('A', 'B', 'C'), loc='best')
                    plt.title(str(each_date))
                    fig.savefig( str(each_date) +'.png')

如果您运行此代码,它似乎不是我想要的。例如:我想在2015年' 2016年' 2017年'上展示单独的图表! x轴将是" 1"," 2"," 3"而y值将是它们的关键值。

当您运行此代码时,图表上的重叠图表显示2016年和2017年的年度图表。

1 个答案:

答案 0 :(得分:0)

好的,所以我稍微重组了一下,似乎有效。关键是在LocationManager locMgr; string tag = "MainActivity"; public override void OnStart () { base.OnStart (); Log.Debug (tag, "OnStart called"); } public override void OnResume () { base.OnResume (); Log.Debug (tag, "OnResume called"); // initialize location manager locMgr = Activity.GetSystemService (Context.LocationService) as LocationManager; string Provider = LocationManager.GpsProvider; //if (locationProvider != null) { if (locMgr.IsProviderEnabled(Provider)) { locMgr.RequestLocationUpdates (Provider, 2000, 1, this); } else { Log.Info (tag, "No location providers available"); } } public override void OnPause () { base.OnPause (); // RemoveUpdates takes a pending intent - here, we pass the current Activity locMgr.RemoveUpdates (this); Log.Debug (tag, "Location updates paused because application is entering the background"); } public override void OnStop () { base.OnStop (); Log.Debug (tag, "OnStop called"); } public void OnLocationChanged (Android.Locations.Location location) { Log.Debug (tag, "Location changed"); var currentCoordinates = new Coordinates (location.Latitude, location.Longitude); LoadData (currentCoordinates); Latitude.Text = "Latitude: " + location.Latitude.ToString(); Longitude.Text = "Longitude: " + location.Longitude.ToString(); } public void OnProviderDisabled (string provider) { Log.Debug (tag, provider + " disabled by user"); } public void OnProviderEnabled (string provider) { Log.Debug (tag, provider + " enabled by user"); } public void OnStatusChanged (string provider, Availability status, Bundle extras) { Log.Debug (tag, provider + " availability has changed to " + status.ToString()); } 循环结束时,您需要清除图形(each_date)。此外,您的循环clf会循环播放所有日期,因此您需要检查日期for d_A, v_A in data_dict_A.items()是否与d_A相同。

each_date

https://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/ enter image description here enter image description here