Pandas DataFrame plotting - Tick labels - python

this is a follow-up question on a piece of code I have posted previously here.
I am plotting a Dataframe object using data_CO2_PBPROD.T.plot(marker='o', color='k', alpha=0.3, lw=2) but I get on the x-axis double labels, as you can see in this picture
I tried to work on the set_major_formatter property of matplotlib.pyplot.axes() but then I get a separate graph with the correct tick labels - but no data displayed - along with the previous graph, unchanged.

You can use the argument xticks to set the values of your axis as explained in the documentation here:
xticks = [date for _ ,date in data_CO2_PBPROD.Column1]
Where Column1 is the column of your DataFrame containing the tuples (Values, Year)
Then put the xticks as a parameter to your plot function :
data_CO2_PBPROD.T.plot(marker='o', xticks=xticks, color='k', alpha=0.3, lw=2)

Related

How to plot multiple lines in subplot using python and matplotlib

I've been following the solutions provided by Merge matplotlib subplots with shared x-axis. See solution 35. In each subplot, there is one line, but I would like to have multiple lines in each subplot. For example, the top plot has the price of IBM and a 30 day moving average. The bottom plot has a 180 day and 30 day variance.
To plot multiple lines in my other python programs I used (data).plot(figsize=(10, 7)) where data is a dataframe indexed by date, but in the author's solution he uses line0, = ax0.plot(x, y, color='r') to assign the data series (x,y) to the plot. In the case of multiple lines in solution 35, how does one assign a dataframe with multiple columns to the plot?
You'll need to use (data).plot(ax=ax0) to work with pandas plotting.
For the legend you can use:
handles0, labels0 = ax0.get_legend_handles_labels()
handles1, labels1 = ax1.get_legend_handles_labels()
ax0.legend(handles=handles0 + handles1, labels=labels0 + labels1)

Why I am getting two different plots for same data?

I have a data file. (duration : One day)
I want to plot time vs temp graph.
I tried to plot it using two different block of codes. The methods are standard but I am getting two different plots.
In first method I used time in datetime datatype
#method
t=pd.to_datetime(data['time'],format='%H:%M:%S.%f').dt.time
data['time']=t
data.drop('index',axis=1,inplace=True)
ax=data.plot(x='time',color=['tab:blue','tab:red'])
ax.tick_params(axis='x', rotation=45)
In second method I used time in string format
fig, ax = plt.subplots()
ax.plot(data['time'], data['temp'])
ax.plot(data['time'], data['temp_mean'],color='red')
fig.autofmt_xdate()
ax.tick_params(axis='x', rotation=45)
y1,y2=float(data.temp.min()),float(data.temp.max())
ax.yaxis.set_ticks(np.arange(y1,y2),10)
ax.set_xlabel('Time')
ax.set_ylabel('Temp')
plt.show()
The second plot is the expected one. But the one I got have overlapped x ticks. I need to have xticks in hourly format (1 hr frequency)
Why I am getting plots with two different pattern? How to add xticks with 1 hour frequency?

how to convert 168 data points to hourly plot data for weekdays

I have a data frame as:
and I can plot this data as :
how can I make x axis of this plot like the following plot:
fig, ax1 = plt.subplots(figsize=(15, 5))
ax1.set(xlabel='hours in a week', ylabel='occupancy ratio(0-1)')
ax1.plot(HoursOfWeek.values, color='g')
plt.show()
After plotting the graph, you can edit the x-ticks. Documentation:
plt.xticks()
For the markers,
plt.xticks(np.r_[0:15]*7, ['00','12']*7+['00'])
For the x-axis labels, plt.xlabel or plt.text should do the job.
Documentation: plt.xlabel
I suggest you use this method to write the x-label:
How to put text outside python plots?(ImportanceOfBeingErnest's answer)

How to use a 3rd dataframe column as x axis ticks/labels in matplotlib scatter

I'm struggling to wrap my head around matplotlib with dataframes today. I see lots of solutions but I'm struggling to relate them to my needs. I think I may need to start over. Let's see what you think.
I have a dataframe (ephem) with 4 columns - Time, Date, Altitude & Azimuth.
I produce a scatter for alt & az using:
chart = plt.scatter(ephem.Azimuth, ephem.Altitude, marker='x', color='black', s=8)
What's the most efficient way to set the values in the Time column as the labels/ticks on the x axis?
So:
the scale/gridlines etc all remain the same
the chart still plots alt and az
the y axis ticks/labels remain as is
only the x axis ticks/labels are changed to the Time column.
Thanks
This isn't by any means the cleanest piece of code but the following works for me:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(ephem.Azimuth, ephem.Altitude, marker='x', color='black', s=8)
labels = list(ephem.Time)
ax.set_xticklabels(labels)
plt.show()
Here you will explicitly force the set_xticklabels to the dataframe Time column which you have.
In other words, you want to change the x-axis tick labels using a list of values.
labels = ephem.Time.tolist()
# make your plot and before calling plt.show()
# insert the following two lines
ax = plt.gca()
ax.set_xticklabels(labels = labels)
plt.show()

matplotlib.pyplot: how to include custom legends when plotting dataframes?

I am plotting two dataframes in the same chart: the USDEUR exchange rate and the 3-day moving average.
df.plot(ax=ax, linewidth=1)
rolling_mean.plot(ax=ax, linewidth=1)
Both dataframes are labelled "Value" so I would like to customize that:
I tried passing the label option but that didn't work, as it seems that this option is exclusive to matplotlib.axes.Axes.plot and not to pandas.DataFrame.plot. So I tried using axes instead, and passing each label:
ax.plot(df, linewidth=1, label='FRED/DEXUSEU')
ax.plot(rolling_mean, linewidth=1, label='3-day SMA')
However now the legend is not showing up at all unless I explicitly call ax.legend() afterwards.
Is it possible to plot the dataframes while passing custom labels without the need of an additional explicit call?
When setting a label using df.plot() you have to specifiy the data which is being plotted:
fig, (ax1, ax2) = plt.subplots(1,2)
df = pd.DataFrame({'Value':np.random.randn(10)})
df2 = pd.DataFrame({'Value':np.random.randn(10)})
df.plot(label="Test",ax=ax1)
df2.plot(ax=ax1)
df.plot(y="Value", label="Test",ax=ax2)
df2.plot(y="Value", ax=ax2)
ax1.set_title("Reproduce problem")
ax2.set_title("Possible solution")
plt.show()
Which gives:
Update: It appears that there is a difference between plotting a dataframe, and plotting a series. When plotting a dataframe, the labels are taken from the column names. However, when specifying y="Value" you are then plotting a series, which then actually uses the label argument.

Categories

Resources