透過 chatgpt 生成的程式碼-凯发ag旗舰厅 二维码
发表时间:2023-05-01 22:43 測試一:讀取資料並基礎呈現圖表import pandas as pd import matplotlib.pyplot as plt # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 繪製長條圖 plt.barh(top_10_df['name'], top_10_df['dollar_price']) plt.gca().invert_yaxis() # 反轉y軸,以使最高的名字顯示在最上面 plt.xlabel('dollar_price') plt.title('2020 top 10 big mac index') plt.show() 測試二:將圖表進行優化 import pandas as pd import seaborn as sns # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 繪製長條圖 sns.set_style("darkgrid") plt.figure(figsize=(10, 6)) sns.barplot(x='dollar_price', y='name', data=top_10_df, palette='rocket_r') plt.xlabel('dollar price', fontsize=14, weight='bold') plt.ylabel('name', fontsize=14, weight='bold') plt.title('2020 top 10 big mac index', fontsize=16, weight='bold') plt.tick_params(axis='both', labelsize=12) plt.show() 測試三:圖表變化import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 設定圖表樣式 sns.set_style('whitegrid') plt.figure(figsize=(10,6)) # 繪製長條圖 ax = sns.barplot(x='dollar_price', y='name', data=top_10_df, palette='spectral_r') # 加入標籤及標題 ax.set_title('2020 top 10 big mac index', fontsize=18) ax.set_xlabel('dollar price', fontsize=14) ax.set_ylabel('name', fontsize=14) # 設定y軸字型大小 ax.tick_params(axis='y', labelsize=12) # 加入x軸刻度線標籤 for i in ax.containers: ax.bar_label(i, label=f"${i.get_width():.2f}", label_type='edge', fontsize=10) plt.show() --------------------------------------------------------------------------- attributeerror traceback (most recent call last)
attributeerror: 'barcontainer' object has no attribute 'get_width' 測試四:口語指定配色import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 設定圖表樣式 sns.set_style('whitegrid') plt.figure(figsize=(10,6)) # 繪製長條圖 colors = ['#ffffff', '#bc002d'] ax = sns.barplot(x='dollar_price', y='name', data=top_10_df, palette=colors) # 加入標籤及標題 ax.set_title('2020 top 10 big mac index', fontsize=18) ax.set_xlabel('dollar price', fontsize=14) ax.set_ylabel('name', fontsize=14) # 設定y軸字型大小 ax.tick_params(axis='y', labelsize=12) # 加入x軸刻度線標籤 for i in ax.containers: ax.bar_label(i, label=f"${i.get_xy()[0] i.get_width():.2f}", label_type='edge', fontsize=10) plt.show() --------------------------------------------------------------------------- attributeerror traceback (most recent call last)
attributeerror: 'barcontainer' object has no attribute 'get_xy' import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 設定圖表樣式 sns.set_style('whitegrid') plt.figure(figsize=(10,6)) # 繪製長條圖 colors = ['#005bbb', '#ffd500'] ax = sns.barplot(x='dollar_price', y='name', data=top_10_df, palette=colors) # 加入標籤及標題 ax.set_title('2020 top 10 big mac index', fontsize=18) ax.set_xlabel('dollar price', fontsize=14) ax.set_ylabel('name', fontsize=14) # 設定y軸字型大小 ax.tick_params(axis='y', labelsize=12) # 加入x軸刻度線標籤 for i in ax.containers: ax.bar_label(i, label=f"${i.get_xy()[0] i.get_width():.2f}", label_type='edge', fontsize=10) plt.show() --------------------------------------------------------------------------- attributeerror traceback (most recent call last)
attributeerror: 'barcontainer' object has no attribute 'get_xy' 測試五:生成圖import pandas as pd import folium # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選出 date='2020-07-01' 且 dollar_price 為量化數值的資料 df_filtered = df[(df['date'] == '2020-07-01') & (df['dollar_price'].notnull())] # 排序取前十筆資料 df_sorted = df_filtered.sort_values(by='dollar_price', ascending=false).head(10) # 創建地圖 m = folium.map(location=[30, 0], zoom_start=2) # 加入國旗 icon icon_size = {1: 20, 2: 30, 3: 40, 4: 50, 5: 60} for i, row in df_sorted.iterrows(): folium.marker( location=[row['lat'], row['lon']], tooltip=f"{row['name']} - {row['dollar_price']}", icon=folium.icon(icon='flag', icon_color='white', prefix='fa', extra_classes='fa-2x', icon_size=icon_size.get(row['dollar_price'], 20)) ).add_to(m) # 設定標題 title_html = ''' 2020 top 10 big mac index ''' m.get_root().html.add_child(folium.element(title_html)) # 顯示地圖 m --------------------------------------------------------------------------- keyerror traceback (most recent call last) /usr/local/lib/python3.9/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3801try:-> 3802return self._engine.get_loc(casted_key) 3803except keyerror as err: pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.pyobjecthashtable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.pyobjecthashtable.get_item() keyerror: 'lat' the above exception was the direct cause of the following exception: keyerror traceback (most recent call last) /usr/local/lib/python3.9/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3802return self._engine.get_loc(casted_key) 3803except keyerror as err:-> 3804raise keyerror(key)from err import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 讀取資料 url = 'https://drive.google.com/file/d/1v-t2u8ksqkitlviidqcraokzslga_cqd/view?usp=sharing' file_id = url.split('/')[-2] dwn_url = 'https://drive.google.com/uc?id=' file_id df = pd.read_csv(dwn_url) # 篩選資料 date_filter = df['date'] == '2020-07-01' filtered_df = df[date_filter] top_10_df = filtered_df.nlargest(10, 'dollar_price') # 設定圖表樣式 sns.set_style('whitegrid') sns.set_palette(['#fbcd00', '#d90000']) plt.figure(figsize=(10,6)) # 繪製長條圖 ax = sns.barplot(x='dollar_price', y='name', data=top_10_df) # 加入標籤及標題 ax.set_title('2020 top 10 big mac index', fontsize=18) ax.set_xlabel('dollar price', fontsize=14) ax.set_ylabel('name', fontsize=14) # 設定y軸字型大小 ax.tick_params(axis='y', labelsize=12) plt.show() ' |