绘制分位数,子图

时间:2018-12-17 17:10:58

标签: python pandas matplotlib

我正在尝试解决edx上似乎无法解决的问题。

尽管显示系统的图形显示“您是否两次调用plt.show()?”

在输出中它也显示以下内容:

array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f86702755c0>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7f867020dbe0>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7f867022c198>],
      dtype=object)

任何帮助将不胜感激。谢谢

  • 绘制数据框。
    • 使用函数plt.xticks()清理x轴标签。
    • 设置第一个参数等于np.arange(len(sal_quantiles.index)), 第二个参数等于sal_quantiles.index和关键字 参数rotation ='垂直'。
    • 显示情节。现在,使用参数subplots = True调用.plot()方法,以在单独的轴上绘制列。也显示此图。

# Plot the data
sal_quantiles.plot()

# Set xticks
plt.xticks(
    np.arange(len(sal_quantiles.index)),
    sal_quantiles.index, 
    rotation='vertical')

# Show the plot
plt.show()

# Plot with subplots
sal_quantiles.plot(subplots=True)

基于先前的练习... 数据源:https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/recent-grads.csv

  1. 绘制工资分位数,第1部分

现在您有兴趣绘制一些不同的平均分位数 主要类别的薪水,以便您可以比较不同的分布 工资。在本练习中,您将为matplotlib准备数据。

说明

中值,p25th和p75th列当前被编码为字符串。转换这些 列到数值。然后,将每列的值除以1000,得出 最终图的比例更易于阅读。

找到每个主要类别的三列中的每一列。将此另存为sal_quantiles


import pandas as pd

# Convert to numeric and divide by 1000

recent_grads['median'] = pd.to_numeric(recent_grads['median'])/ 1000

recent_grads['p25th'] = pd.to_numeric(recent_grads['p25th'])/ 1000

recent_grads['p75th'] = pd.to_numeric(recent_grads['p75th'])/ 1000



# Select averages by major category

columns = ['median', 'p25th', 'p75th']

sal_quantiles = recent_grads.groupby(['major_category'])['median', 'p25th', 'p75th'].mean()

print(sal_quantiles)

1 个答案:

答案 0 :(得分:0)

应用plt.show()通过评估后,您需要再次显示图像,即subplot=True

相关问题