How do I define an area to plot in Pandas?

Now I am trying to build a dataframe in Pandas. Everything is fine, but I don't know how to define the y-axes and x-axes. For example, in the following, I want to display a graph from 1.0 to 0.0 on the y-axis scale instead of 0.0 to 0.7.

enter image description here

Here is the code for the above graph.

In [90]: df
Out[90]: 
             history       lit   science    social  accuracy
2014-11-18  0.680851  0.634146  0.452381  0.595745      0.01
2014-12-10  0.680851  0.634146  0.452381  0.595745      0.01

In [91]: df.plot()
Out[91]: <matplotlib.axes._subplots.AxesSubplot at 0x7f9f3e7c9410>

      

Also, I want to show the 'x' marker for each point. For example, DataFrame df has two lines, so I want to mark "x" or "o" for each point in the graph.

Updated:

After applying the excellent Ffisegydd solution, I got the following graph that I originally wanted.

In [6]: df.plot(ylim=(0,1), marker='x')

      

enter image description here

+3


source to share


1 answer


pandas.DataFrame.plot()

will return the matplotlib axes object. This can be used to change things like y-limits using ax.set_ylim()

.

Alternatively, when you call df.plot()

, you can pass arguments for the style, one of those arguments can be ylim=(minimum_value, maximum_value)

, that is, you don't have to manually use ax.set_ylim()

after printing. In the meantime, there is no need to know about it. ”

You can also pass additional keyword arguments that are passed to the matplotlib plot routine, you can use this to set the marker as x

with marker='x'

.



Below is a toy example where ylim was set to (0,5)

and the token to x

in the call df.plot()

.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(data={'x':[0,1,2,3,4], 'y':[0,0.5,1,1.5,2]})

ax = df.plot(ylim=(0,5), marker='x')

plt.show()

      

Example plot

+2


source







All Articles