Mapplotlib输出问题

时间:2018-02-23 02:56:16

标签: python

所以我正在为学校做一个项目,并在这里完成一个完整的面孔。我的词典是有序的,我正在按照我的意愿将数值转换为浮点数,但是当Mapplot渲染它时,数字在图形上按字母顺序排列(即:1111,1112,1333333,20)。我不能为我的生活弄清楚如何解决它而且让我感到沮丧。

我希望以正确的数字顺序显示值(降序)并且无法使其工作。

import sqlite3
import re
import requests
from math import pi
from itertools import chain
from collections import namedtuple
from bokeh.io import show, output_notebook
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool, FuncTickFormatter, FixedTicker, ColumnDataSource
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict
from pandas import DataFrame
from collections import Counter
from operator import itemgetter

token = ''
header = headers = {'Authorization': f'Bearer {token}','Content-type': 'application/json'}

steam_hours = requests.get('https://query.data.world/s/5cOVc-nvDK4DkUJ2MFZuAqCcAqTJrN', 
                             headers=headers)
raw_data = steam_hours.text

steam_list = raw_data.split('\r\n')
#print(steam_list)
steam_list[2]



game_hours = [r.split('|')[-1] for r in steam_list[1:]]
game_name = [r.split('|')[0] for r in steam_list[1:]]

game_hours_edit = [i.replace('"', '') for i in game_hours]

def remove_comma(s):
    return s[1:]

def remove_comma_name(s):
    return s[:-1]



game_hours_edit = [remove_comma(s) for s in game_hours_edit]
game_hours = game_hours_edit

game_name_edit = [remove_comma_name(s) for s in game_name]
game_name = game_name_edit

games_dict = OrderedDict(zip(game_name, game_hours))
print(games_dict)

#key, value = ('asd', 1)

print('\n')
for key, value in games_dict.items():
    print('\n')
    print(key, value)
print('\n')


scrubbed = [re.sub(r'[^\x00-\x7F]+',' ', game_hours) for game_hours in game_hours]
#print(f"Game Name: {game_name}\n Hours Played: {scrubbed}")

conn = sqlite3.connect('steam_hours.db')
cur = conn.cursor() 
cur.execute('''drop table IF EXISTS name_game''')
cur.execute('''CREATE TABLE IF NOT EXISTS name_game (game_name TEXT, game_hours TEXT)''')

for key, value in games_dict.items():
    #print(game_name)
    #print(game_hours)
    cur.execute('INSERT OR REPLACE INTO name_game VALUES (?, ?)', (key, value,))
    conn.commit()

name_results = cur.execute('SELECT game_name FROM name_game LIMIT 10;').fetchall()
hours_results = cur.execute('SELECT game_hours FROM name_game DESC LIMIT 10;').fetchall()


all_compiled = pd.read_sql_query("SELECT * from name_game", conn)
name_compiled = pd.read_sql_query("SELECT game_name from name_game", conn)
hours_compiled = pd.read_sql_query("SELECT game_hours from name_game", conn)




names_flat = [item for sublist in name_results for item in sublist]
hours_flat = [item for sublist in hours_results for item in sublist]

list_int = np.array(hours_flat)

print(list_int)

names_reversed = names_flat[::-1]
hours_reversed = hours_flat[::-1]

from collections import OrderedDict

final_dict = OrderedDict(zip(list_int, names_flat ))

print("final dict")
print(final_dict)

print("FOR LOOP")
print('\n')
for key, value in final_dict.items():
    print('\n')
    print(key, value)
print('\n')

fig = plt.figure()
fig.suptitle('TOP 10 STEAM GAMES BY HOURS PLAYED', fontsize=14, fontweight='bold')
plt.xticks(rotation=15, fontsize=8)
bar_width = 2  # default: 0.8


plt.ylabel('Hours Played', fontsize=8)
plt.xlabel('Game Name', fontsize=8)
plt.bar(range(len(final_dict)), final_dict.keys(), align="center", width=.5)
plt.xticks(range(len(final_dict)),  list(final_dict.values()))

plt.show()

0 个答案:

没有答案