我正在尝试使用matplotlib在pyspark中绘制条形图。但是,在执行plt(x,y)时,出现错误AttributeError:'NoneType'对象没有属性'update'。这里x是一个字符串列表,y是一个整数列表。
>>> import matplotlib.pyplot as plt
>>> x
['SOUTH AFRICA', 'BAHAMAS', 'ARMENIA', 'JAPAN', 'BANGLADESH', 'UGANDA', 'GY1 4HY', 'JERSEY', 'TANZANIA', 'JORDAN', 'MALTA', 'ALBANIA', 'BELARUS', 'LANCS', 'SCOTLAND', 'MAURITIUS', 'KUWAIT', 'TAJIKSTAN', 'LITHUANIA', 'GY1 2HL', 'UNITED KINGDOM', 'AZERBAIJAN', 'EAST SUSSEX', 'PHILIPPINES', 'SAINT KITTS AND NEVIS', 'CZECH REPUBLIC', None, 'OMAN', 'HONG KONG CHINA', 'CV31 3RG', 'FIJI', 'COSTA RICA', 'SOUTH KOREA', 'CANADA', 'ENGLAND UNITED KINGDOM', 'POLAND', 'SLOVAKIA', 'GRENADA', 'SRI LANKA', 'COLOMBIA', 'VANUATU', 'UNITED ARAB EMIRATES', 'ALGERIA', 'VIRGIN ISLANDS', 'GIBRALTAR', 'SOUTH-WEST AFRICA', 'NORTHERN IRELAND UNITED KINGDOM', 'GUERNSEY', 'U', 'ENGLAND', 'DOMINICA', 'LIBERIA', 'MOROCCO', 'DUMMY', 'REUNION', 'MEXICO', 'CAMEROON', 'PAKISTAN', 'ANGUILLA', '1200', 'BURUNDI', 'FRANCE', 'ISLAMIC REPUBLIC OF IRAN', 'CAYMAN ISLANDS', 'GAMBIA', 'BRUNEI DARUSSALAM', 'SAINT LUCIA', 'SINGAPORE', 'MOZAMBIQUE', 'MADAGASCAR', 'CURACAO', 'NIGERIA', 'CHANNEL ISLANDSSW1W 0AU', 'BRITISH VIGIN ISLANDS', 'CHINA', 'AUSTRIA', 'MALAYSIA', 'C', 'ZAMBIA', 'LATVIA', 'NEPAL', 'BOTSWANA', 'SW7', 'KYRGYZSTAN', 'MALI', 'BELIZE', 'ZIMBABWE', 'SAUDI ARABIA', 'CROATIA', 'MARSHALL ISLANDS', 'RWANDA', 'FAROE ISLAND', 'SWITZERLAND', 'BULGARIA', 'KAZAKHSTAN', 'ARGENTINA', 'IVORY COAST', 'SUDAN', 'BRITISH WEST INDIES', 'HONG KONG', 'UKRAINE', 'CZECHOSLOVAKIA', 'SERBIA', 'N', 'BWI', 'NORWAY', 'FINLAND', 'MONGOLIA', 'MALAWI', 'NETHERLANDS', 'WEST INDIES', 'GUYANA', 'SLOVENIA', 'LIVERPOOL', 'SIERRA LEONE', 'UZBEKISTAN', 'IRAN', 'CHANNEL ISLANDS', 'SWEDEN', 'ECUADOR', 'BELGIUM', 'RUSSIA', 'NORTHERN IRELAND', 'TRINIDAD AND TOBAGO', 'NEW ZEALAND', 'CONGO, THE DEMOCRATIC REPUBLIC OF THE', 'CHANNEL ISLES', 'SPAIN', 'MOLDOVA', 'GREECE', 'LEBANON', 'GUINEA', 'INDIA', 'BERMUDA', 'TAIWAN', 'KENYA', 'ICELAND', 'TURKEY', 'COOK ISLANDS', 'FAROE ISLANDS', 'LUXEMBOURG', 'AFGHANISTAN', 'BRITISH CHANNEL ISLANDS', 'YUGOSLAVIA', 'USSR', 'LIECHTENSTEIN', 'TURKS AND CAICOS ISLANDS', 'URUGUAY', 'SOUTH AFRICAN', 'VIRGIN ISLANDS, BRITISH', 'NETHERLANDS ANTILLES', 'ENGLAND & WALES', 'ITALY', 'AUSTRALIA', 'MONACO', 'PANAMA', 'CHILE', 'IRAQ', 'DENMARK', 'THAILAND', 'HUNGARY', 'CHAD', 'QATAR', 'SAINT VINCENT AND THE GRENADINES', 'JAMAICA', 'CONGO', 'VIETNAM', 'CYPRUS', 'SEYCHELLES', 'BRITISH ISLES', 'ETHIOPIA', 'ISRAEL', 'PORTUGAL', 'KOREA', 'INDONESIA', 'YEMEN', 'IRELAND', 'UNITED STATES', 'KY1-9005', 'ROMANIA', 'GERMANY', 'GHANA', 'SCOTLAND UK', 'ISLE OF MAN', 'BRAZIL', 'LIBYA', 'KOSOVO', 'BARBADOS', 'WALES', 'VENEZUELA', 'EGYPT', 'BAHRAIN', 'PERU', 'PO33 2TG', 'GEORGIA']
>>> y
[35, 59, 2, 215, 17, 7, 1, 202, 5, 2, 21, 1, 3, 1, 91225, 21, 9, 2, 3, 1, 1178488, 5, 1, 3, 6, 4, 1834076, 2, 1, 1, 1, 1, 76, 191, 1, 14, 1, 1, 20, 2, 2, 38, 1, 177, 82, 1, 5, 67, 2, 1141835, 7, 31, 4, 1, 1, 6, 1, 29, 3, 1, 1, 179, 1, 359, 4, 1, 1, 76, 2, 1, 4, 80, 1, 1, 96, 22, 30, 1, 6, 3, 1, 2, 1, 1, 1, 10, 12, 12, 3, 14, 2, 1, 361, 5, 8, 3, 2, 2, 1, 116, 6, 3, 1, 1, 2, 38, 20, 2, 2, 360, 1, 2, 2, 1, 1, 4, 24, 350, 39, 1, 99, 25, 21205, 6, 69, 1, 1, 105, 1, 12, 8, 1, 307, 90, 13, 5, 7, 38, 3, 1, 73, 2, 1, 3, 24, 7, 5, 7, 1, 246, 8, 5, 83, 221, 4, 48, 1, 4, 57, 4, 6, 1, 6, 2, 9, 1, 5, 42, 14, 2, 1, 19, 10, 7, 4, 1, 461, 2478, 2, 9, 284, 18, 1, 392, 23, 1, 2, 13, 31984, 3, 11, 11, 1, 1, 2]
>>> plt.bar(x,y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/applied/sparkVenv/lib/python3.6/site-packages/matplotlib/__init__.py", line 1810, in inner
return func(ax, *args, **kwargs)
File "/home/applied/sparkVenv/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 2238, in bar
x = self.convert_xunits(x)
File "/home/applied/sparkVenv/lib/python3.6/site-packages/matplotlib/artist.py", line 186, in convert_xunits
return ax.xaxis.convert_units(x)
File "/home/applied/sparkVenv/lib/python3.6/site-packages/matplotlib/axis.py", line 1530, in convert_units
ret = self.converter.convert(x, self.units, self)
File "/home/applied/sparkVenv/lib/python3.6/site-packages/matplotlib/category.py", line 53, in convert
unit.update(values)
AttributeError: 'NoneType' object has no attribute 'update'
答案 0 :(得分:0)
您的列表中有一个None
值。也将其转换为字符串,例如通过
plt.bar(np.array(x).astype(str),y)
您可以添加plt.tick_params(axis="x", rotation=90)
来旋转标签以使它们不重叠。
完整代码:
import numpy as np
import matplotlib.pyplot as plt
x = ['SOUTH AFRICA', 'BAHAMAS', 'ARMENIA', 'JAPAN', 'BANGLADESH', 'UGANDA', 'GY1 4HY', 'JERSEY', 'TANZANIA', 'JORDAN', 'MALTA', 'ALBANIA', 'BELARUS', 'LANCS', 'SCOTLAND', 'MAURITIUS', 'KUWAIT', 'TAJIKSTAN', 'LITHUANIA', 'GY1 2HL', 'UNITED KINGDOM', 'AZERBAIJAN', 'EAST SUSSEX', 'PHILIPPINES', 'SAINT KITTS AND NEVIS', 'CZECH REPUBLIC', None, 'OMAN', 'HONG KONG CHINA', 'CV31 3RG', 'FIJI', 'COSTA RICA', 'SOUTH KOREA', 'CANADA', 'ENGLAND UNITED KINGDOM', 'POLAND', 'SLOVAKIA', 'GRENADA', 'SRI LANKA', 'COLOMBIA', 'VANUATU', 'UNITED ARAB EMIRATES', 'ALGERIA', 'VIRGIN ISLANDS', 'GIBRALTAR', 'SOUTH-WEST AFRICA', 'NORTHERN IRELAND UNITED KINGDOM', 'GUERNSEY', 'U', 'ENGLAND', 'DOMINICA', 'LIBERIA', 'MOROCCO', 'DUMMY', 'REUNION', 'MEXICO', 'CAMEROON', 'PAKISTAN', 'ANGUILLA', '1200', 'BURUNDI', 'FRANCE', 'ISLAMIC REPUBLIC OF IRAN', 'CAYMAN ISLANDS', 'GAMBIA', 'BRUNEI DARUSSALAM', 'SAINT LUCIA', 'SINGAPORE', 'MOZAMBIQUE', 'MADAGASCAR', 'CURACAO', 'NIGERIA', 'CHANNEL ISLANDSSW1W 0AU', 'BRITISH VIGIN ISLANDS', 'CHINA', 'AUSTRIA', 'MALAYSIA', 'C', 'ZAMBIA', 'LATVIA', 'NEPAL', 'BOTSWANA', 'SW7', 'KYRGYZSTAN', 'MALI', 'BELIZE', 'ZIMBABWE', 'SAUDI ARABIA', 'CROATIA', 'MARSHALL ISLANDS', 'RWANDA', 'FAROE ISLAND', 'SWITZERLAND', 'BULGARIA', 'KAZAKHSTAN', 'ARGENTINA', 'IVORY COAST', 'SUDAN', 'BRITISH WEST INDIES', 'HONG KONG', 'UKRAINE', 'CZECHOSLOVAKIA', 'SERBIA', 'N', 'BWI', 'NORWAY', 'FINLAND', 'MONGOLIA', 'MALAWI', 'NETHERLANDS', 'WEST INDIES', 'GUYANA', 'SLOVENIA', 'LIVERPOOL', 'SIERRA LEONE', 'UZBEKISTAN', 'IRAN', 'CHANNEL ISLANDS', 'SWEDEN', 'ECUADOR', 'BELGIUM', 'RUSSIA', 'NORTHERN IRELAND', 'TRINIDAD AND TOBAGO', 'NEW ZEALAND', 'CONGO, THE DEMOCRATIC REPUBLIC OF THE', 'CHANNEL ISLES', 'SPAIN', 'MOLDOVA', 'GREECE', 'LEBANON', 'GUINEA', 'INDIA', 'BERMUDA', 'TAIWAN', 'KENYA', 'ICELAND', 'TURKEY', 'COOK ISLANDS', 'FAROE ISLANDS', 'LUXEMBOURG', 'AFGHANISTAN', 'BRITISH CHANNEL ISLANDS', 'YUGOSLAVIA', 'USSR', 'LIECHTENSTEIN', 'TURKS AND CAICOS ISLANDS', 'URUGUAY', 'SOUTH AFRICAN', 'VIRGIN ISLANDS, BRITISH', 'NETHERLANDS ANTILLES', 'ENGLAND & WALES', 'ITALY', 'AUSTRALIA', 'MONACO', 'PANAMA', 'CHILE', 'IRAQ', 'DENMARK', 'THAILAND', 'HUNGARY', 'CHAD', 'QATAR', 'SAINT VINCENT AND THE GRENADINES', 'JAMAICA', 'CONGO', 'VIETNAM', 'CYPRUS', 'SEYCHELLES', 'BRITISH ISLES', 'ETHIOPIA', 'ISRAEL', 'PORTUGAL', 'KOREA', 'INDONESIA', 'YEMEN', 'IRELAND', 'UNITED STATES', 'KY1-9005', 'ROMANIA', 'GERMANY', 'GHANA', 'SCOTLAND UK', 'ISLE OF MAN', 'BRAZIL', 'LIBYA', 'KOSOVO', 'BARBADOS', 'WALES', 'VENEZUELA', 'EGYPT', 'BAHRAIN', 'PERU', 'PO33 2TG', 'GEORGIA']
y = [35, 59, 2, 215, 17, 7, 1, 202, 5, 2, 21, 1, 3, 1, 91225, 21, 9, 2, 3, 1, 1178488, 5, 1, 3, 6, 4, 1834076, 2, 1, 1, 1, 1, 76, 191, 1, 14, 1, 1, 20, 2, 2, 38, 1, 177, 82, 1, 5, 67, 2, 1141835, 7, 31, 4, 1, 1, 6, 1, 29, 3, 1, 1, 179, 1, 359, 4, 1, 1, 76, 2, 1, 4, 80, 1, 1, 96, 22, 30, 1, 6, 3, 1, 2, 1, 1, 1, 10, 12, 12, 3, 14, 2, 1, 361, 5, 8, 3, 2, 2, 1, 116, 6, 3, 1, 1, 2, 38, 20, 2, 2, 360, 1, 2, 2, 1, 1, 4, 24, 350, 39, 1, 99, 25, 21205, 6, 69, 1, 1, 105, 1, 12, 8, 1, 307, 90, 13, 5, 7, 38, 3, 1, 73, 2, 1, 3, 24, 7, 5, 7, 1, 246, 8, 5, 83, 221, 4, 48, 1, 4, 57, 4, 6, 1, 6, 2, 9, 1, 5, 42, 14, 2, 1, 19, 10, 7, 4, 1, 461, 2478, 2, 9, 284, 18, 1, 392, 23, 1, 2, 13, 31984, 3, 11, 11, 1, 1, 2]
plt.bar(np.array(x).astype(str),y)
plt.tick_params(axis="x", rotation=90)
plt.show()