Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing index as xticks for pandas plot

I have the following dataframe and am trying to plot it, so that it shows in the x-axis the index data from 8-19.

If I do df.plot() no labels are shown at all. If I do df.plot(use_index=True), the behaviour is unchanged. Finally I tried df.plot(xticks=df.index) but I'm getting an error AttributeError: 'NoneType' object has no attribute 'seq'

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
null = np.nan

df = pd.DataFrame.from_dict({"today sensor 1": {"08": 22.9, "09": 22.7, "10": 22.8, "11": 23.6, "12": 24.1, "13": 24.9,
                                           "14": 25.0, "15": 25.2, "16": 25.7, "17": 26.1, "18": 26.0, "19": 25.8},
                        "today sensor 2": {"08": 24.5, "09": 24.5, "10": 24.8, "11": 25.3, "12": 26.4, "13": 26.7,
                                           "14": 27.1, "15": 27.6, "16": 28.0, "17": 28.0, "18": 28.2, "19": 28.0},
                        "yesterday sensor 1": {"08": null, "09": null, "10": null, "11": null, "12": null, "13": null,
                                               "14": null, "15": null, "16": 23.0, "17": 23.6, "18": 23.5, "19": 23.5},
                        "yesterday sensor 2": {"08": null, "09": null, "10": null, "11": null, "12": null, "13": null,
                                               "14": null, "15": null, "16": 24.8, "17": 24.9, "18": 24.9, "19": 24.8}})

# df.plot(use_index=True)  # does not work
df.plot(xticks=df.index)

plt.show()

What is even more strange is that when I do this:

ax = df.plot(use_index=True, style=['bs-', 'go-', 'b:', 'g:'])
ax.set_xticklabels(df.index)
plt.show()

The xticks will show but they are wrong. Only numbers from 9-14 are snown and every other number only. I would expect 08-19 as xticks.

Any suggestions are appreciated.

like image 819
Nickpick Avatar asked Jun 11 '18 20:06

Nickpick


People also ask

How do you annotate a panda plot?

Create a figure and a set of subplots using subplots() method. Plot a series of data frame using plot() method, kind='scatter', ax=ax, c='red' and marker='x'. To annotate the scatter point with the index value, iterate the data frame. To display the figure, use show() method.

What are pandas Xticks?

xticks : sequence. Values to use for the xticks. yticks : sequence. Values to use for the yticks. xlim : 2-tuple/list.

How do I add a label to a plot in pandas?

You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.

How do you find the index of a panda element?

The get_loc() function is used to find the index of any column in the Python pandas dataframe.


1 Answers

Until the bug in Pandas gets fixed, add this after df.plot(), no need for anything else:

plt.xticks(range(len(df.index)), df.index)
like image 142
Tronic Avatar answered Nov 15 '22 20:11

Tronic