📊 Charts in Python with Plotly

  

Plotly is a library that lets you create beautiful + interactive graphs in Python.

Interactive means:

  • you can zoom
  • you can hover over points
  • you can turn series on/off
  • you can export to PNG/HTML by clicking a button

You don't need to know JavaScript or anything else: just write Python and it does the rest.

🔗 Do you like Techelopment? Check out the site for all the details!

Installation (easy)

From the terminal:

pip install plotly

No other complicated dependencies: it works right away.


First graphs: the "bare minimum" philosophy

Plotly has two ways to create graphs:

  1. Plotly Express (px) → simple and fast
  2. Graph Objects (go) → more customization, but more code

For now, we use Plotly Express because it covers 80% of the cases and is very easy.


1. Line chart

Simple example (data inside the code)

import plotly.express as px

fig = px.line(x=[1, 2, 3], y=[10, 20, 15], title="Simple trend")
fig.show()

What's happening:

  • x → values ​​on the horizontal axis
  • y → values ​​on the vertical axis
  • title → title of the chart

Plotly figures it all out.


2. Bar Chart

import plotly.express as px

fig = px.bar(x=["A", "B", "C"], y=[3, 5, 2], title="Simple Bars")
fig.show()

Perfect for comparing categories.


3. Scatter Chart

import plotly.express as px

fig = px.scatter(x=[1,2,3,4], y=[10,8,12,15], title="Scatter base")
fig.show()

You can also use it to see correlations.


4. Adding colors, sizes, and labels (Plotly Express does this almost automatically)

Plotly becomes more powerful when you give it more information.

import plotly.express as px

fig = px.scatter(
   x=[1,2,3,4],
   y=[10,8,12,15],
   color=["red","blue","red","blue"],
   size=[10,20,30,15],
   title="Scatter with colors and sizes"
)
fig.show()

What does color= do?
It divides the points into groups and automatically colors them.

What does size= do?
It changes the size of the points.


5. Quick chart customization

You can change titles, labels, dimensions, and themes—all in a few lines.

fig.update_layout(
   title="Improved Title",
   xaxis_title="X-Axis",
   yaxis_title="Y-Axis",
   template="plotly_dark"
)

The template parameter changes the chart's style.
Some useful templates:

  • plotly
  • plotly_dark
  • ggplot2
  • seaborn
  • simple_white

6. Using a CSV file with Plotly

This is one of the most common cases.

import pandas as pd
import plotly.express as px

df = pd.read_csv("data.csv")

fig = px.line(df, x="data", y="value")
fig.show()

Plotly Express integrates seamlessly with pandas.


7. Taking it to the next level: Graph Objects

If you want more control (exact colors, dashed lines, multiple series, etc.), use go.Figure().

Slightly more advanced example:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
   x=[1,2,3],
   y=[5,15,10],
   mode="lines+markers",
   name="Series 1",
   line=dict(width=3, dash="dash")
))

fig.update_layout(title="Advanced Graph")
fig.show()

Here you can control EVERYTHING.


8. Save the graph

Save as HTML (preserves interactivity)

fig.write_html("grafico.html")

Save as image (PNG, JPG, SVG)

Requires Kaleido (very lightweight):

pip install kaleido

Then:

fig.write_image("grafico.png")

🌈 Summary

Plotly Express (px):

  • Quick graphs
  • Code simple
  • Perfect for everyday use

Graph Objects (go):

  • Total control
  • Perfect for complex presentations/layouts




Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment