<jupyter_start><jupyter_text>Plotting with seabornScmData provides limited support for plotting. However, we make it as easy as possible to return data in a format which can be used with the [seaborn](https://seaborn.pydata.org/index.html) plotting library. Given the power of this library, we recommend having a look through its documentation if you want to make anything more than the most basic plots.<jupyter_code># NBVAL_IGNORE_OUTPUT
import matplotlib.pyplot as plt
import seaborn as sns
from scmdata.run import ScmRun
from scmdata.plotting import RCMIP_SCENARIO_COLOURS<jupyter_output><empty_output><jupyter_text>DataFor this notebook we use the RCMIP radiative forcings, available at rcmip.org.<jupyter_code># NBVAL_IGNORE_OUTPUT
rcmip_db = ScmRun("rcmip-radiative-forcing-annual-means-v4-0-0.csv")
rcmip_db.head()<jupyter_output><empty_output><jupyter_text>Plotting with ScmRunFor the most common plotting patterns, we provide a very simple `lineplot` method in `ScmRun`.<jupyter_code># NBVAL_IGNORE_OUTPUT
out = rcmip_db.filter(variable="Effective Radiative Forcing").lineplot()
out<jupyter_output><empty_output><jupyter_text>``kwargs`` passed to this method are given directly to [``seaborn.lineplot``](https://seaborn.pydata.org/generated/seaborn.lineplot.html), which allows an extra layer of control.For example, we can plot on slightly bigger axes, make the lines slightly transparent, add markers for the different models, specify the colour to use for each scenario and specify the order to display the scenarios in.<jupyter_code># NBVAL_IGNORE_OUTPUT
ax = plt.figure(figsize=(16, 9)).add_subplot(111)
rcmip_db.filter(variable="Effective Radiative Forcing").lineplot(
    ax=ax,
    hue="scenario",
    palette=RCMIP_SCENARIO_COLOURS,
    hue_order=RCMIP_SCENARIO_COLOURS.keys(),
    style="model",
    alpha=0.7,
)<jupyter_output><empty_output><jupyter_text>Specifying the time axisPlotting with a `datetime.datetime` time axis is not always convenient. To address this, we provide the `time_axis` keyword argument. The options are available in the `lineplot` docstring.<jupyter_code>print(rcmip_db.lineplot.__doc__)
# NBVAL_IGNORE_OUTPUT
fig, axes = plt.subplots(figsize=(16, 9), nrows=2, ncols=2)

pdb = rcmip_db.filter(variable="Effective Radiative Forcing")
for ax, time_axis in zip(
    axes.flatten(),
    [
        "year",
        "year-month",
        "days since 1970-01-01",
        "seconds since 1970-01-01",
    ],
):
    pdb.lineplot(
        ax=ax,
        hue="scenario",
        palette=RCMIP_SCENARIO_COLOURS,
        hue_order=RCMIP_SCENARIO_COLOURS.keys(),
        style="model",
        alpha=0.7,
        time_axis=time_axis,
        legend=False,
    )
    ax.set_title(time_axis)

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>These same options can also be passed to the `timeseries` and `long_data` methods.<jupyter_code># NBVAL_IGNORE_OUTPUT
rcmip_db.timeseries(time_axis="year-month")
# NBVAL_IGNORE_OUTPUT
rcmip_db.long_data(time_axis="days since 1970-01-01")<jupyter_output><empty_output><jupyter_text>Plotting with seabornIf you wish to make plots which are more complex than this most basic pattern, a combination of seaborn and pandas reshaping is your best bet.  Plotting on a gridOften we wish to look at lots of different variables at once. Seaborn allows this sort of 'gridded' plotting, as shown below.<jupyter_code>vars_to_plot = ["Effective Radiative Forcing"] + [
    "Effective Radiative Forcing|{}".format(v)
    for v in [
        "Anthropogenic",
        "Anthropogenic|Aerosols",
        "Anthropogenic|CO2",
        "Anthropogenic|CH4",
        "Anthropogenic|N2O",
    ]
]
vars_to_plot
# NBVAL_IGNORE_OUTPUT
seaborn_df = rcmip_db.filter(variable=vars_to_plot).long_data()
seaborn_df.head()<jupyter_output><empty_output><jupyter_text>With the output of `.long_data()` we can directly use [``seaborn.relplot``](https://seaborn.pydata.org/generated/seaborn.relplot.html).<jupyter_code># NBVAL_IGNORE_OUTPUT
sns.relplot(
    data=seaborn_df,
    x="time",
    y="value",
    col="variable",
    col_wrap=3,
    hue="scenario",
    palette=RCMIP_SCENARIO_COLOURS,
    hue_order=RCMIP_SCENARIO_COLOURS.keys(),
    alpha=0.7,
    facet_kws={"sharey": False},
    kind="line",
)<jupyter_output><empty_output><jupyter_text>Variable scatter plotsSometimes we don't want to plot against time, rather we want to plot variables against each other. For example, we might want to see how the effective radiative forcings relate to each other in the different scenarios. In such a case we can reshape the data using pandas before using seaborn.<jupyter_code># NBVAL_IGNORE_OUTPUT
ts = rcmip_db.filter(variable=vars_to_plot[:4]).timeseries()
ts.head()
# NBVAL_IGNORE_OUTPUT
ts_reshaped = ts.unstack("variable").stack("time").reset_index()
ts_reshaped.head()
# NBVAL_IGNORE_OUTPUT
sns.pairplot(
    ts_reshaped,
    hue="scenario",
    palette=RCMIP_SCENARIO_COLOURS,
    hue_order=RCMIP_SCENARIO_COLOURS.keys(),
    corner=True,
    height=4,
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10001984520738241, 'fraction_numerical': 0.009922603691208573, 'mean_word_length': 5.184049079754601, 'pattern_counts': {'":': 1, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '96717', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1729, 'n_words': 424, 'path': 'notebooks/plotting-with-seaborn.ipynb', 'repo_name': 'lewisjared/scmdataframe', 'size': '5039'}