Home >Backend Development >Python Tutorial >What are the commonly used Python data visualization libraries?
What library would you use for data visualization in Python?
Today I will share with you a powerful member of the Python data visualization library-Altair!
It is very simple, friendly, and built on the powerful Vega-Lite JSON specification. We only need short code to generate beautiful and effective visualizations.
Altair is a statistical visualization Python library that currently has more than 3,000 stars on GitHub.
With Altair, we can focus more energy and time on understanding the data itself and its meaning, and be freed from the complex data visualization process.
Simply put, Altair is a visual grammar and a declarative language for creating, saving and sharing interactive visual designs. It can use JSON format to describe the visual appearance and interaction process, and generate network-based image.
Let’s take a look at the visualization effects made using Altair!
#Altair's advantagesAltair can comprehensively understand, understand and analyze data through aggregation, data transformation, data interaction, graphic composite and other methods. These processes can help us increase our understanding of the data itself and its meaning, and cultivate intuitive data analysis thinking. In general, the characteristics of Altair include the following aspects.import altair as alt import pandas as pd data = pd.read_excel( "Index_Chart_Altair.xlsx", sheet_name="Sales", parse_dates=["Year"] ) alt.Chart( data )Quick test - make a bar chartAltair places great emphasis on variables Differentiation and combination of types. The value of a variable is data, and there are differences. It can be expressed in the form of numerical values, strings, dates, etc. Variables are storage containers for data, and data are the contents of the storage units of variables. On the other hand, from the perspective of statistical sampling, the variable is the population and the data is the sample. Samples need to be used to study and analyze the population. Statistical graphs can be generated by combining different variable types with each other to provide a more intuitive understanding of the data. According to the combination of different variable types, the combination of variable types can be divided into the following types.
chart = alt.Chart(df).mark_bar().encode(x="profit:Q",y="product:N")Complex graphs are also very simple Let’s demonstrate the average monthly rainfall in different years by partition!
我们可以使用面积图描述西雅图从2012 年到2015 年的每个月的平均降雨量统计情况。接下来,进一步拆分平均降雨量,以年份为分区标准,使用阶梯图将具体年份的每月平均降雨量分区展示,如下图所示。
核心的实现代码如下所示。
… chart = alt.Chart(df).mark_area( color="lightblue", interpolate="step", line=True, opacity=0.8 ).encode( alt.X("month(date):T", axis=alt.Axis(format="%b", formatType="time", labelAngle=-15, labelBaseline="top", labelPadding=5, title="month")), y="mean(precipitation):Q", facet=alt.Facet("year(date):Q", columns=4, header=alt.Header( labelColor="red", labelFontSize=15, title="Seattle Monthly Precipitation from 2012 to 2015", titleFont="Calibri", titleFontSize=25, titlePadding=15) ) 0) …
在类alt.X()中,使用month 提取时间型变量date 的月份,映射在位置通道x轴上,使用汇总函数mean()计算平均降雨量,使用折线作为编码数据的标记样式。
在实例方法encode()中,使用子区通道facet 设置分区,使用year 提取时间型变量date 的年份,作为拆分从2012 年到2015 年每个月的平均降雨量的分区标准,从而将每年的不同月份的平均降雨量分别显示在对应的子区上。使用关键字参数columns设置子区的列数,使用关键字参数header 设置子区序号和子区标题的相关文本内容。
具体而言,使用Header 架构包装器设置文本内容,也就是使用类alt.Header()的关键字参数完成文本内容的设置任务,关键字参数的含义如下所示。
The above is the detailed content of What are the commonly used Python data visualization libraries?. For more information, please follow other related articles on the PHP Chinese website!