Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ggplot2 in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use ggplot2 in Python? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Plotnine

Loyal users of R know that ggplot2 can make it easier for you to deal with exploratory data analysis and data visualization. It makes it so easy to create elegant and powerful plots that help interpret the underlying relationships in the data.

Do Python users have similar drawing tools?

The answer to this question is in Plotnine.

The style of Plotnine is 99% similar to that of ggplot2 in R, with the main difference being the use of parentheses, as you will see in the following short examples. One of the best gains from using plotnine is that the output is basically the same as what you got in R. There is no significant visual difference.

Let's take a brief look at how to use Plotnine.

Installation:

Pip install pandas plotnine

Let's set up the work environment with the necessary libraries and load the csv file into a data framework called survs_df:

Import numpy as npimport pandas as pdfrom plotnine import *

% matplotlib inlinesurvs_df = pd.read_csv ('surveys.csv') .dropna ()

To generate a graph using the ggplot class in plotnine, we must provide three things:

The data box that contains our data.

How to convert the columns of a data frame to the position, color, size, and shape of graphic elements (Aesthetics).

The actual graphic elements to display ("geometric objects")

Ggplot (survs_df, aes (x-weights, y-weights, footprints) + geom_point ()

For API in plotnine, we can use a number of options to create graphics.

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

One of the main selling points of ggplot in R is the ability of FACET. We also have many options for drawing a subset of data with one line of code:

(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='Cylinders')) + facet_wrap ('~ gear')

This is the answer to the question about how to use ggplot2 in Python. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report