Search code examples
pythonpandasgraphjupyter-notebookplotly

How to fix the axes of a graph for plotly


I am creating a graph for Tokyo which plots rent over time. It has worked when I plot date against US dollars. However, when I plot against the local currency (Yen), the order of they Y-Axis gets messed up. I'm not so sure how to fix it.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
%matplotlib inline
df = pd.read_csv('Tokyo rent (One Bedroom apartment in the city centre).csv')
fig = px.line(df, x = 'Date 2.0', y = 'Rent(USD)', title='Tokyo Rent Price (USD)')
fig.show()

The Y axis is in good order

fig = px.line(df, x = 'Date 2.0', y = 'Rent(Local Currency)', title='Tokyo Rent Price (Yen)')
fig.show()

The Y axis order is messed up

My CSV file

Year    Rent(USD)   Date    Rent(Local Currency)    Date 2.0
2017    1223.4  17/03/17    129,594.59              2017-03-17
2017    1070.24 28/10/17    121,656.25              2017-10-28
2018    1104.51 23/01/18    121,689.66              2018-01-23
2018    1030.61 22/10/18    116,270.83              2018-10-22
2019    1124.33 14/06/19    122,062.50              2019-06-14
2019    1129.6  20/06/19    121,255.32              2019-06-20
2019    1129.9  21/06/19    121,255.32              2019-06-21
2020    1198.53 23/03/20    128,701.75              2020-03-23
2020    1183.66 01/07/20    127,195.65              2020-07-01
2020    1213.38 17/09/20    127,466.67              2020-09-17
2020    1168.37 05/10/20    123,578.95              2020-10-05
2020    1192.5  11/11/20    125,525.00              2020-11-11
2020    1228.34 02/12/20    128,312.50              2020-12-02
2021    1220    06/03/21    132,200.00              2021-03-06
2021    1342.84 29/08/21    147,524.40              2021-08-29
2021    1284.65 14/10/21    145,696.54              2021-10-14

Solution

  • The commas in the Rent(Local Currency) values caused pandas to import them as strings. Probably the best solution is to add the thousands=',' argument to read_csv:

    df = pd.read_csv('Tokyo rent (One Bedroom apartment in the city centre).csv', thousands=',')