温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在python中使用ggplot画图

发布时间:2021-01-27 15:28:56 来源:亿速云 阅读:359 作者:Leah 栏目:开发技术

如何在python中使用ggplot画图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

简介:

ggplot类是在plotnine中的,能够生成一个图形。

安装:

pip install pandas plotnine

csv文件加载到survs_df的数据框架:

ggplot(survs_df, aes(x='weight', y='hindfoot_length',
size = 'year')) + geom_point()

生成图形步骤:

1、设置数据框

2、需要将数据框架转换成位置、颜色、大小等

3、显示实际图形元素

实例代码:

(ggplot(mtcars, aes(‘wt', ‘mpg', color='factor(cyl)'))
+ geom_point()
+ labs(title='Miles per gallon vs Weight', x='Weight', y='Miles per gallon')
+ guides(color=guide_legend(title='Number of Cylinders')) )

输出效果:

如何在python中使用ggplot画图

知识点扩展:

绘制散点图,geom_point()

读取外部数据进行绘图

>>> import pandas as pd
>>> from ggplot import *
>>> df=pd.read_table('C:\Users\lenovo\Desktop\mtcars.txt')
>>> df
··name type number volume size other
0 td T 96 3 20 c
1 sf F 87 5 65 c
2 cc F 79 9 80 d

如果读取的数据没有column可以添加

df.columns=['name','type','number','volume','size','other']
>>> p=ggplot(df, aes(x='number', y='volume', size='factor(size)', color='factor(other)'))+geom_point()
>>> print p

关于如何在python中使用ggplot画图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI