Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中DF的水平条形图_Python_Dataframe_Csv_Graph_Bar Chart - Fatal编程技术网

Python中DF的水平条形图

Python中DF的水平条形图,python,dataframe,csv,graph,bar-chart,Python,Dataframe,Csv,Graph,Bar Chart,所以我一直在尝试用Python中的DF制作一个水平条形图。我成功了,但X轴中的值顺序不正确 import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) plt.rcdefaults() fig, ax = plt.subplots() plt.xlim(0, 14) # data people = (a.Ethn

所以我一直在尝试用Python中的DF制作一个水平条形图。我成功了,但X轴中的值顺序不正确

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


plt.rcdefaults()
fig, ax = plt.subplots()

plt.xlim(0, 14)

# data
people = (a.Ethnicity)
y_pos = np.arange(len(people))
performance = a.Value

ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Value')
ax.set_title('Unemployment between different ethnic groups in 2004')

plt.show()
这段代码给出了下图:

但我希望它是这样的: 或者像这样:

这是变量a:

a = df.loc[(df.Time == 2018) & (df.Region == "All") & (df.Age == "All") & (df.Sex == "All"), ["Ethnicity","Value"]]
print(a)
                      Ethnicity Value
32772                        All   4.2
32952                      Asian   6.2
33132                Asian Other   6.1
33312                      Black   8.8
33492                     Indian   4.3
33672                      Mixed     7
33852                      Other   7.5
34032           Other than White   7.1
34212  Pakistani and Bangladeshi   8.4
34392                    Unknown   4.1
34572                      White   3.7
34752              White British   3.8
34932                White Other   3.4

我在我的编辑器中复制并粘贴了你的代码,得到了你想要的图形。X轴中的值顺序正确。在我看来,可能在代码的另一部分发生了某种不正确的排序。我将您的数据复制为字典以创建数据框

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

fig, ax = plt.subplots()
plt.xlim(0, 100)

data = {'All': 4.2,
        'Asian': 6.2,
        'Asian Other': 6.1,
        'Black': 8.8,
        'Indian': 4.3,
        'Mixed': 7,
        'Other': 7.5,
        'Other than White': 7.1,
        'Pakistani and Bangladeshi': 8.4,
        'Unknown': 4.1,
        'White': 3.7,
        'White British': 3.8,
        'White Other': 3.4}

a = pd.DataFrame(data.items(), columns=["Ethnicity", "Value"])
people = (a.Ethnicity)
y_pos = np.arange(len(people))
performance = a.Value

ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Value')

plt.tight_layout()
plt.show()
结果:


我在编辑器中复制并粘贴了您的代码,得到了您想要的图形。X轴中的值顺序正确。在我看来,可能在代码的另一部分发生了某种不正确的排序。我将您的数据复制为字典以创建数据框

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

fig, ax = plt.subplots()
plt.xlim(0, 100)

data = {'All': 4.2,
        'Asian': 6.2,
        'Asian Other': 6.1,
        'Black': 8.8,
        'Indian': 4.3,
        'Mixed': 7,
        'Other': 7.5,
        'Other than White': 7.1,
        'Pakistani and Bangladeshi': 8.4,
        'Unknown': 4.1,
        'White': 3.7,
        'White British': 3.8,
        'White Other': 3.4}

a = pd.DataFrame(data.items(), columns=["Ethnicity", "Value"])
people = (a.Ethnicity)
y_pos = np.arange(len(people))
performance = a.Value

ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Value')

plt.tight_layout()
plt.show()
结果:


这很有效。但那是因为它来自字典。但我如何从数据框中做到这一点呢?图形中的Y轴值恰好与问题中打印的数据框的顺序相同。尝试重置数据帧的索引,看看它是否改变了什么。如果没有,那么您可以查看
pandas.DataFrame.to_dict
,这是一个函数,允许您将数据帧转换为字典,然后绘制它。这很有效。但那是因为它来自字典。但我如何从数据框中做到这一点呢?图形中的Y轴值恰好与问题中打印的数据框的顺序相同。尝试重置数据帧的索引,看看它是否改变了什么。如果没有,那么您可以查看
pandas.DataFrame.to_dict
,该函数允许您将数据帧转换为字典,然后绘制该字典。