用Python画散点图和直方图的操作分别是什么
Admin 2022-08-09 群英技术资讯 1223 次浏览
在实际应用中,我们有时候会遇到“用Python画散点图和直方图的操作分别是什么”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“用Python画散点图和直方图的操作分别是什么”文章能帮助大家解决问题。实现功能:
在散点图上添加趋势线(线性拟合线)反映两个变量是正相关、负相关或者无相关关系。
实现代码:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings(action='once')
plt.style.use('seaborn-whitegrid')
sns.set_style("whitegrid")
print(mpl.__version__)
print(sns.__version__)
def draw_scatter(file):
# Import Data
df = pd.read_csv(file)
df_select = df.loc[df.cyl.isin([4, 8]), :]
# Plot
gridobj = sns.lmplot(
x="displ",
y="hwy",
hue="cyl",
data=df_select,
height=7,
aspect=1.6,
palette='Set1',
scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))
# Decorations
sns.set(style="whitegrid", font_scale=1.5)
gridobj.set(xlim=(0.5, 7.5), ylim=(10, 50))
gridobj.fig.set_size_inches(10, 6)
plt.tight_layout()
plt.title("Scatterplot with line of best fit grouped by number of cylinders")
plt.show()
draw_scatter("F:\数据杂坛\datasets\mpg_ggplot2.csv")
实现效果:

在散点图上添加趋势线(线性拟合线)反映两个变量是正相关、负相关或者无相关关系。红蓝两组数据分别绘制出最佳的线性拟合线。
实现功能:
python绘制边缘直方图,用于展示X和Y之间的关系、及X和Y的单变量分布情况,常用于数据探索分析。
实现代码:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings(action='once')
plt.style.use('seaborn-whitegrid')
sns.set_style("whitegrid")
print(mpl.__version__)
print(sns.__version__)
def draw_Marginal_Histogram(file):
# Import Data
df = pd.read_csv(file)
# Create Fig and gridspec
fig = plt.figure(figsize=(10, 6), dpi=100)
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
# Define the axes
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
# Scatterplot on main ax
ax_main.scatter('displ',
'hwy',
s=df.cty * 4,
c=df.manufacturer.astype('category').cat.codes,
alpha=.9,
data=df,
cmap="Set1",
edgecolors='gray',
linewidths=.5)
# histogram on the right
ax_bottom.hist(df.displ,
40,
histtype='stepfilled',
orientation='vertical',
color='#098154')
ax_bottom.invert_yaxis()
# histogram in the bottom
ax_right.hist(df.hwy,
40,
histtype='stepfilled',
orientation='horizontal',
color='#098154')
# Decorations
ax_main.set(title='Scatterplot with Histograms \n displ vs hwy',
xlabel='displ',
ylabel='hwy')
ax_main.title.set_fontsize(10)
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] +
ax_main.get_xticklabels() + ax_main.get_yticklabels()):
item.set_fontsize(10)
xlabels = ax_main.get_xticks().tolist()
ax_main.set_xticklabels(xlabels)
plt.show()
draw_Marginal_Histogram("F:\数据杂坛\datasets\mpg_ggplot2.csv")
实现效果:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
模块可以看成是一堆函数的集合体。一个py文件内部就可以放一堆函数,因此一个py文件就可以看成一个模块。如果这个py文件的文件名为module.py,模块名则是module。
这篇文章主要介绍了Numpy中的ravel_multi_index函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这篇文章主要为大家介绍了如何利用Python语言实现在Excel文件中写入一个比较简单的图表,文中的实现方法讲解详细,快动手尝试一下吧
这篇文章主要介绍了利用Python实现读取二进制文件,文章尝试使用Python中的内置open函数使用默认读取模式读取zip文件,下文详细介绍,需要的小伙伴可以参考一下
这篇文章主要为大家介绍了Python之可迭代对象,迭代器和生成器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008