密谋从 pandas 数据框中格式化海图 [英] plotting & formatting seaborn chart from pandas dataframe

查看:52
本文介绍了密谋从 pandas 数据框中格式化海图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框al_df,其中包含最近一次美国人口普查所得的阿拉巴马州人口.我创建了一个使用seaborn绘制的累积函数,得出了以下图表:

I have a pandas dataframe al_df that contains the population of Alabama from a recent US census. I created a cumulative function that I plot using seaborn, resulting in this chart:

与绘图有关的代码是这样的:

The code that relates to the plotting is this:

figure(num=None, figsize=(20, 10))

plt.title('Cumulative Distribution Function for ALABAMA population')
plt.xlabel('City')
plt.ylabel('Percentage')
#sns.set_style("whitegrid", {"ytick.major.size": "0.1",})
plt.plot(al_df.pop_cum_perc)

我的问题是: 1)如何更改刻度,以便y轴每0.1单位显示一条网格线,而不是显示的默认0.2线? 2)如何更改x轴以显示垂直绘制的城市实际名称,而不是城市的等级"(来自Pandas索引)? (有300多个名称,因此它们在水平方向上不太合适).

My questions are: 1) How can I change the ticks, so the yaxis shows a grid line every 0.1 units instead of the default 0.2 shown? 2) How can I change the x axis to show the actual names of the city, plotted vertically, instead of the "rank" of the city (from the Pandas index)? (there are over 300 names, so they are not going to fit well horizontally).

推荐答案

在进行了一些研究之后,由于找不到本地" Seaborn解决方案,我提出了以下代码,部分基于@Pablo Reyes和@朱朱的建议,以及使用matplotlib函数:

After some research, and not been able to find a "native" Seaborn solution, I came up with the code below, partially based on @Pablo Reyes and @CT Zhu suggestions, and using matplotlib functions:

from matplotlib.ticker import *
figure(num=None, figsize=(20, 10))

plt.title('Cumulative Distribution Function for ALABAMA population')
plt.xlabel('City')
plt.ylabel('Percentage')
plt.plot(al_df.pop_cum_perc)

#set the tick size of y axis
ax = plt.gca()
ax.yaxis.set_major_locator(MultipleLocator(0.1))

#set the labels of y axis and text orientation
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.set_xticklabels(labels, rotation =90)

该解决方案引入了一个新元素标签",我必须在情节之前指定它,作为从我的Pandas数据框创建的新Python列表:

The solution introduced a new element "labels" which I had to specify before the plot, as a new Python list created from my Pandas dataframe:

labels = al_df.NAME.values[:]

产生以下图表:

这需要进行一些调整,因为在熊猫数据框中指定了每个城市的显示,如下所示:

This requires some tweaking, since specifying a display of every city in the pandas data frame, like this:

ax.xaxis.set_major_locator(MultipleLocator(1))

产生无法读取的图表(仅显示x轴):

Produces a chart impossible to read (displaying only x axis):

这篇关于密谋从 pandas 数据框中格式化海图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆