Skip to content

Commit

Permalink
Merge pull request #256 from JosephBARBIERDARNAL/homepage-pandas
Browse files Browse the repository at this point in the history
pandas homepage
  • Loading branch information
holtzy authored Oct 9, 2023
2 parents a35c0b2 + f7ab719 commit 2ff4a3b
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 69 deletions.
254 changes: 185 additions & 69 deletions src/pages/pandas.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Container from 'react-bootstrap/Container';
import Contact from '../components/Contact';
import Row from 'react-bootstrap/Row';
import ChartFamilySection from '../components/ChartFamilySection';
import ChartImage from '../components/ChartImage';
import { Link } from 'gatsby';
import { Plotly } from '../components/MiscellaneousLogos';
import { Pandas } from '../components/MiscellaneousLogos';
import { Col } from 'react-bootstrap';
import CodeChunk from '../components/CodeChunk';
import Spacing from '../components/Spacing';
Expand All @@ -16,13 +17,44 @@ import ChartImageContainer from '../components/ChartImageContainer';
const chartDescription = (
<>
<p>
Beyond its powerful data manipulation capabilities, Pandas offers
convenient plotting methods, enabling users to visualize data directly
from DataFrame and Series objects.
Pandas is a popular open-source Python
library used for data manipulation and analysis. It provides <b>data structures</b> and functions
that make working with structured data, such as tabular data (like <code>Excel</code> spreadsheets or
<code>SQL</code> tables), easy and intuitive.
</p>
<p>
Although not best known for this functionality, Pandas can be used to create graphs and visualize
data, thanks to its <b>lightweight syntax</b> and <a href='https://python-graph-gallery.com/matplotlib/'>matplotlib</a> functions.
</p>
</>
);

const quickCode = `# library
import pandas as pd
import matplotlib.pyplot as plt
# Create data
values=[12, 11, 9, 13, 14, 16, 14, 15, 18, 17, 19, 20]
df=pd.DataFrame({'x': range(1,13), 'y': values })
# plot
df.plot('x', 'y')
plt.show()
`;

const plotApi = `df.plot('x', 'y', kind='line')
plt.show()
`;

const funcnameApi = `df.line('x', 'y')
plt.show()
`;

const plotFuncnameApi = `df.plot.line('x', 'y')
plt.show()
`;


export default function Plotlys() {
return (
<Layout
Expand All @@ -37,7 +69,155 @@ export default function Plotlys() {
/>

<Container>
<p>This section is under construction and will be available soon.</p>
<h2 id="Quick">&#9201; Quick start</h2>
<Row className="align-items-center">
<Col md={6}>
<p>
<code>Pandas</code> is the most famous python data
manipulation and cleaning library. However, it can also be used to
create charts and graphs. Pandas plotting features are a wrapper
around the <a href="https://python-graph-gallery.com/matplotlib/">matplotlib</a>{' '}
library, which is the most popular python library for data visualization.
</p>
<p>
The <code>plot</code> function is the most basic function to
create a chart with pandas. It is a wrapper around the{' '}
<code>matplotlib.pyplot.plot</code> function.
</p>
</Col>
<Col md={6}>
<Link to={'/530-introduction-to-linechart-with-pandas'}>
<ChartImage
imgName="quick-pandas"
caption="The most basic (line) chart one can make with pandas"
/>
</Link>
</Col>
</Row>
<CodeChunk>{quickCode}</CodeChunk>
</Container>

<Spacing />

<Container>
<h2 id="APIs">
<Pandas />
Three distinct syntaxes
</h2>
<p>
There are 3 ways to build a chart with pandas: the{' '}
<code>plot</code> method, the <code>function name</code> method
(like <i>line</i>, <i>bar</i> or <i>hist</i>) and the <code>plot + function name</code> method.
</p>
<p>
➡️ <code>plot method</code>
</p>
<p>
In this case, we have to specify the <code>kind</code> of chart we
want to create. The <code>plot</code> method is a wrapper around the{' '}
<code>matplotlib.pyplot.plot</code> function. The <code>kind</code>{' '}
argument is used to specify the <b>type of chart</b> we want to create.
</p>
<CodeChunk>{plotApi}</CodeChunk>
<br />
<br />
<p>
➡️ <code>function name method</code>
</p>
<p>
The function name method is a bit more straightforward. We just have
to call <b>the right function name</b> to create the chart we want. Matplotlib has
various functions to create different types of charts. For example, the
<code>line</code> function is used to create line charts.
</p>

<CodeChunk>{funcnameApi}</CodeChunk>

<br />
<br />
<p>
➡️ <code>plot + function name method</code>
</p>
<p>
This method is a combination of the previous two. We use the{' '}
<code>plot</code> method and need the <code>function name</code>
right after it.
</p>

<CodeChunk>{plotFuncnameApi}</CodeChunk>

<br />
<br />

<p>
The <code>function name</code> method is the most straightforward and
the one we recommend. <b>Most posts</b> on the gallery use this method.
</p>

</Container>

<Spacing />

<Container>
<h2 id="Example">
<Pandas />
Chart examples with Pandas
</h2>
<p>
Pandas offers a wide range of nice charts. Here is a <b>selection of
examples</b> that you can find on the gallery. Click on the images to see the code!
</p>
<Row>
<ChartImageContainer
imgName="527-introduction-to-histogram-with-pandas"
caption="Simple histogram built with pandas"
linkTo="/527-introduction-to-histogram-with-pandas"
/>
<ChartImageContainer
imgName="529-multi-group-histogram-pandas"
caption="Multi group histogram"
linkTo="/529-multi-group-histogram-pandas"
/>
<ChartImageContainer
imgName="529-multi-group-histogram-pandas-2"
caption="Small multiples histogram"
linkTo="/529-multi-group-histogram-pandas"
/>
</Row>
<Row>
<ChartImageContainer
imgName="535-introduction-to-scatter-plot-with-pandas"
caption="Scatter plot with pandas"
linkTo="/535-introduction-to-scatter-plot-with-pandas"
/>
<ChartImageContainer
imgName="537-scatter-plots-grouped-by-color-with-pandas"
caption="Scatter plot grouped by color"
linkTo="/537-scatter-plots-grouped-by-color-with-pandas"
/>
<ChartImageContainer
imgName="536-customizing-scatter-plots-with-pandas"
caption="Customized scatter plot"
linkTo="/536-customizing-scatter-plots-with-pandas"
/>
</Row>
<Row>
<ChartImageContainer
imgName="538-introduction-to-barplot-with-pandas"
caption="Barplot with pandas"
linkTo="/538-introduction-to-barplot-with-pandas"
/>
<ChartImageContainer
imgName="539-customizing-barplot-with-pandas"
caption="Customized barplot"
linkTo="/539-customizing-barplot-with-pandas"
/>
<ChartImageContainer
imgName="540-barplots-grouped-by-color-with-pandas"
caption="Barplot grouped by color"
linkTo="/540-barplots-grouped-by-color-with-pandas"
/>
</Row>
</Container>

<Spacing />
Expand All @@ -60,70 +240,6 @@ export default function Plotlys() {
);
}

const quickCode = `# Load plotly
import plotly.graph_objects as go
# Sample data
x = [1.5, 2.9, 3, 4.2, 5.6]
y = [2.2, 13.3, 4.4, 55.3, 52.1]
# Initialize a figure
fig = go.Figure()
# Add the scatter trace
fig.add_trace(go.Scatter(
x=x, # Variable in the x-axis
y=y, # Variable in the y-axis
mode='markers', # This explicitly states that we want our observations to be represented by points
))
# Show
fig.show()
`;

const codeInstall = `pip install plotly`;

const saveCode = `fig.write_html("the/path/to/chart-name.html")`;
const embedCode = `<iframe
src="the/path/to/chart-name.html"
width="800"
height="600"
title="chart name"
style="border:none">
</iframe>`;

const plotlyExpressCode = `# import the plotly express library
import plotly.express as px

# Some dummy data
categories = ['A', 'B', 'C', 'D', 'E']
values = [15, 22, 18, 12, 28]
# Plot
fig = px.bar(
x=categories,
y=values,
)
fig.show()
`;

const plotlyGoCode = `# import the plotly graph objects lib
import plotly.graph_objects as go
# Some dummy data
categories = ['A', 'B', 'C', 'D', 'E']
values = [15, 22, 18, 12, 28]
# Create a bar chart using the Graph Object API
fig = go.Figure(data=[go.Bar(x=categories, y=values)])
# Update layout
fig.update_layout(
title="Simple Bar Chart",
xaxis_title="Categories",
yaxis_title="Values")
fig.show()
`;
Binary file added static/graph/quick-pandas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2ff4a3b

Please sign in to comment.