Python Graph在Jupyter笔记本中运行良好,但在pycharm中不起作用
试图在python中创建可视化,但在jupyter笔记本上工作时,我无法在实际的IDE中获得所需的输出。这发生在其他人身上了吗?两者的代码完全相同。jupyter在1个单元格中运行(字面上只是复制和粘贴) PyCharm:Python Graph在Jupyter笔记本中运行良好,但在pycharm中不起作用,python,matplotlib,pycharm,Python,Matplotlib,Pycharm,试图在python中创建可视化,但在jupyter笔记本上工作时,我无法在实际的IDE中获得所需的输出。这发生在其他人身上了吗?两者的代码完全相同。jupyter在1个单元格中运行(字面上只是复制和粘贴) PyCharm: import pandas as pd from matplotlib import pyplot as plt #also tried import matplotlib.pyplot as plt from datetime import date, timedelta
import pandas as pd
from matplotlib import pyplot as plt #also tried import matplotlib.pyplot as plt
from datetime import date, timedelta
fig = plt.figure(figsize=(12, 7))
df = pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/main/data/countries-aggregated.csv',
parse_dates=['Date'])
yesterday = date.today() - timedelta(days=1)
yesterday.strftime('%Y-%m-%d')
today_df = df[df['Date'] == yesterday]
top_10 = today_df.sort_values(['Confirmed'], ascending=False)[:10]
top_10.loc['rest-of-world'] = today_df.sort_values(['Confirmed'], ascending=False)[10:].sum()
top_10.loc['rest-of-world', 'Country'] = 'Rest of World'
ax = fig.add_subplot(111)
ax.pie(top_10['Confirmed'], labels=top_10['Country'], autopct='%1.1f%%')
ax.title.set_text('Hardest Hit Countries Worldwide')
plt.legend(loc='upper left')
plt.show()
Jupyter:
import pandas as pd
from matplotlib import pyplot as plt #also tried import matplotlib.pyplot as plt
from datetime import date, timedelta
fig = plt.figure(figsize=(12, 7))
df = pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/main/data/countries-aggregated.csv',
parse_dates=['Date'])
yesterday = date.today() - timedelta(days=1)
yesterday.strftime('%Y-%m-%d')
today_df = df[df['Date'] == yesterday]
top_10 = today_df.sort_values(['Confirmed'], ascending=False)[:10]
top_10.loc['rest-of-world'] = today_df.sort_values(['Confirmed'], ascending=False)[10:].sum()
top_10.loc['rest-of-world', 'Country'] = 'Rest of World'
ax = fig.add_subplot(111)
ax.pie(top_10['Confirmed'], labels=top_10['Country'], autopct='%1.1f%%')
ax.title.set_text('Hardest Hit Countries Worldwide')
plt.legend(loc='upper left')
plt.show()
我在没有IDE的情况下测试了代码,它给出了相同的错误图 它还显示它需要
ax.pie()
中的normalize=True
,所以我添加了它
但它仍然没有解决主要问题
如果您使用print()
查看变量中的值,那么今天可能会看到空的并且它会生成错误的绘图
首先,您昨天忘记分配。strftime(“%Y-%m-%d”)
但最奇怪的问题是我的时区。我住在一个地方,昨天
给出的日期在CSV
中还不存在,我不得不使用timedelta(days=2)
(前天)来获取一些数据并查看绘图。
可能在几个小时后,他们会在CSV
中更新数据,timedelta(days=1)
会工作几个小时,之后会再次出现同样的问题,等等
更好地利用
yesterday = max(df['Date'])
获取最新数据
IDE
是开发代码的唯一工具-在开发之后,您可以在没有IDE
的情况下运行代码-所以更重要的是,它是否可以直接在console/terminalpython script.py
中运行。也许当你们在控制台中运行它时,它会显示一些错误信息,可以帮助你们发现问题。
import pandas as pd
from matplotlib import pyplot as plt
from datetime import date, timedelta
url = 'https://raw.githubusercontent.com/datasets/covid-19/main/data/countries-aggregated.csv'
fig = plt.figure(figsize=(12, 7))
df = pd.read_csv(url, parse_dates=['Date'])
#yesterday = date.today() - timedelta(days=2)
#yesterday = yesterday.strftime('%Y-%m-%d')
yesterday = max(df['Date'])
print('yesterday:', yesterday)
today_df = df[df['Date'] == yesterday]
print(today_df)
top_10 = today_df.sort_values(['Confirmed'], ascending=False)[:10]
top_10.loc['rest-of-world'] = today_df.sort_values(['Confirmed'], ascending=False)[10:].sum()
top_10.loc['rest-of-world', 'Country'] = 'Rest of World'
ax = fig.add_subplot(111)
ax.pie(top_10['Confirmed'], labels=top_10['Country'], autopct='%1.1f%%', normalize=True)
ax.title.set_text('Hardest Hit Countries Worldwide')
plt.legend(loc='upper left')
plt.show()