Hello everyone !
Today, let's talk about how to use one line of code in DataFrame Generate cool dynamic interactive charts in the dataset , Let's first introduce the modules we need to use this time cufflinks
Involving installation , direct pip install that will do
pip install cufflinks
We import the module , Let's see what the current version is
cf.__version__
output
'0.17.3'
At present, the version of the module has reached 0.17.3, It's also the latest version , Then, what charts can be drawn in our latest version
cf.help()
output
Use 'cufflinks.help(figure)' to see the list of available parameters for the given figure. Use 'DataFrame.iplot(kind=figure)' to plot the respective figure Figures: bar box bubble bubble3d candle choroplet distplot .......
From the output above we can see , The general syntax for drawing a chart is df.iplot(kind= Chart name ) How do we want to view the parameters when drawing a specific chart , For example, a histogram bar What are the parameters , You can do that
cf.help('bar')Let's first look at the drawing of histogram chart , First, let's create a data set for chart drawing
df2 = pd.DataFrame({'Category':['A','B','C','D'],
'Values':[95,56,70,85]})
df2output
Category Values 0 A 95 1 B 56 2 C 70 3 D 85
Then let's draw the histogram
df2.iplot(kind='bar',x='Category',y='Values', xTitle = "Category",yTitle = "Values", title = " Histogram ")
output
Among them x The parameter is filled with x The corresponding variable name on the axis , and y The parameter is filled in y The corresponding variable name on the axis , We can draw the chart in png Download it in the format of ,
At the same time, we can also zoom in on the chart ,
Let's take a look at the following set of data
df = pd.DataFrame(np.random.randn(100,4),columns='A B C D'.split()) df.head()
output
A B C D 0 0.612403 -0.029236 -0.595502 0.027722 1 1.167609 1.528045 -0.498168 -0.221060 2 -1.338883 -0.732692 0.935410 0.338740 3 1.662209 0.269750 -1.026117 -0.858472 4 1.387077 -0.839192 -0.562382 -0.989672
Let's plot the histogram
df.head(10).iplot('bar')output
We can also draw “ Stacked ” Histogram
df.head(10).iplot(kind='bar',barmode='stack')
output
So again , We can also draw the histogram horizontally
df.head(10).iplot(kind='barh',barmode='stack')
output
Now let's take a look at the drawing of line chart , Let's start with the above df The columns of the dataset are accumulated
df3 = df.cumsum()
Then let's draw a line chart
df3.iplot()
output
Of course, you can also filter out a few columns and draw them , The effect is as follows
df3[["A", "B"]].iplot()
output
We can also draw a straight line to fit its trend ,
df3['A'].iplot(bestfit = True,bestfit_colors=['pink'])
output
Here we will focus on introducing a iplot() Parameters commonly used in methods
kind: Chart type , The default is scatter, Scatter type , There are other types to choose from bar( Histogram )、box( Box figure )、heatmap( Heat map ) wait theme: Layout theme , Can pass cf.getThemes() To see what are the main title: The title of the chart xTitle/yTitle: x perhaps y The name of the shaft above the shaft colors: The color when drawing the chart subplots: Boolean value , When drawing subgraphs, you need , The default is Falsemode: character string , Drawing mode , There can be lines、markers, There's also lines+markers and lines+text Equal mode size: For scatter charts , It is mainly used to adjust the size of scatter points shape: When drawing subgraphs, the layout of each graph bargap: The distance between columns in the histogram barmode : The shape of histogram ,stack( Stacked )、group( Clusters )、overlay( Cover ) The transition from line graph to area graph is very simple , Only the parameters fill Set to True that will do , The code is as follows
df3.iplot(fill = True)
output