【问题标题】:is there a limit on how many matplotlib plots shown in a single run?一次运行中显示多少个 matplotlib 图有限制吗?
【发布时间】:2017-10-03 19:11:15
【问题描述】:

我正在分析 FreeCodeCamp1 获取的 2016 年调查数据。 https://github.com/freeCodeCamp/2016-new-coder-survey

特别是 2016-new-coder-survey/clean-data/2016-FCC-New-Coders-Survey-Data.csv

由于某种原因,我想添加的任何其他图都不会显示。给出错误

raise AttributeError('Unknown property %s' % k)
AttributeError: Unknown property type

我的代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data_file = pd.read_csv('FCC_New_Coders_Survey_Data.csv', dtype={'AttendedBootcamp': float, 'CodeEventOther': object, 'JobRoleInterestOther': object})
AttendedBootcamp = data_file['AttendedBootcamp']
BootcampFullJobAfter = data_file['BootcampFullJobAfter']
BootcampRecommend = data_file['BootcampRecommend']
BootcampFinish = data_file['BootcampFinish']
Age = data_file['Age']
NetworkID = data_file['NetworkID']

AttendYes = data_file[data_file.AttendedBootcamp == 1]
AgeAttend = AttendYes['Age']
FinishYes = data_file[data_file.BootcampFinish == 1]
FinishNo = data_file[data_file.BootcampFinish == 0]
JobYes = data_file[data_file.BootcampFullJobAfter == 1]
JobNo = data_file[data_file.BootcampFullJobAfter == 0]
RecYes = data_file[data_file.BootcampRecommend == 1]
RecNo = data_file[data_file.BootcampRecommend == 0]

var = [len(JobYes[JobYes.Age == i]) - len(JobNo[JobNo.Age == i]) for i in range(16, 60)]
x = range(16, 60)  
y = var

fig = plt.figure(figsize=(8,8))
plt.plot(x, y, 'go')
plt.xlabel('Age')
plt.ylabel('Net Employment Difference (count)')
plt.title('Employement Discrepencies')
plt.xticks(x)
plt.xscale('linear')
ax = fig.add_subplot(1, 1, 1)
ax.spines['bottom'].set_position('zero')
plt.vlines(x, [0], y)           
ax.xaxis.set_ticks(np.arange(15, 65, 5))
plt.xlabel('Age', horizontalalignment='center', verticalalignment='center', x=1.05)
plt.show()

plt.figure(figsize=(8,8))
plt.title('bootcamp job')
plt.hist([JobYes['Age'], JobNo['Age']], histtype='bar', bins = 44, range=[16,60], label=['Job after camp', 'no job after camp'])
plt.xlabel('Age')
plt.ylabel('Count')
plt.legend()


plt.figure(figsize=(8,8))
plt.title('Attend bootcamp')
plt.hist(AttendYes['Age'], histtype='bar', range=[16,60])
plt.xlabel('Age')
plt.ylabel('Count')
plt.legend()


plt.figure(figsize=(8,8))
plt.title('bootcampfinish')
plt.hist([FinishYes['Age'], FinishNo['Age']], type='bar', range=[16,60], label=['finished', 'didn\'t finish'])
plt.xlabel('Age')
plt.ylabel('Count')
plt.legend()





var1 = [len(RecYes[RecYes.Age == j]) - len(RecNo[RecNo.Age == j]) for j in range(16, 60)]
x1 = range(16, 60)  
y1 = var1
plt.figure(figsize=(8,8))
plt.plot(x1, y1)
plt.xlabel('Age')
plt.ylabel('Net reccomendation (count)')
plt.title('Age Sentiment')
plt.xticks(x1)
plt.xscale('linear')
plt.legend()
plt.show()

如果我要注释掉前四个情节之一,那么将显示第五个情节。

【问题讨论】:

  • matplotlib 中的图形数量没有硬性限制。有一个 rcParameter figure.max_open_warning,默认设置为 20,所以一旦打开超过 20 个图形,就会出现警告。然而,仍然会显示更多的数字,只要内存允许。因此,错误的原因是不同的,可能与您的第 5 个图的参数之一有关。

标签: python matplotlib plot


【解决方案1】:

您的问题不在于地块的数量,而是在调用第五个地块时出现拼写错误。据我所知,情节的数量没有限制(我猜除了你自己的电脑内存)

在您的第五个情节中,您调用了plt.hist(..., type='bar'),而前四个情节应该是plt.hist(..., histtype='bar')

【讨论】:

  • 哇哦!我什至没听懂(你的意思是说第四个情节顺便说一句),现在工作正常。谢谢!那么为什么即使我的语法错误,它仍然显示第四个情节?
猜你喜欢
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
相关资源
最近更新 更多