This question already has answers here:
Changing the tick frequency on the x or y axis
(13 answers)
Closed 4 years ago.
Hi I have a dataframe with dates as index as shares as columns
I want to plot a particular share using dates as x axis.
The series I want to plot is :
df['APPL']=
Date
2018-10-29 24.903347
2018-10-30 25.165384
2018-10-31 25.087744
2018-11-01 24.777180
...
2018-12-06 25.709999
But when I plot it out with df['APPL'].plot(use_index=True), the xasix is not showing.
Then I tried plt.plot(df.index,df['APPL']), the interval of x axis is too small for it to be read. .
How can I increase the interval to show every 10 days for example?
You can rotate:
plt.xticks(rotation=90)
Demo:
plt.plot(df.index,df['APPL'])
plt.xticks(rotation=90)
After rotating, the text would be rotated to 90 degrees.
And if you prefer:
plt.plot(df.index,df['APPL'])
plt.xticks(rotation=45)
Test which you like.
Found to the solution, this code changes the interval to 3 days.
Kudos to #U9-Forward and #wwii for pointing out my index was not datetime format.
df.index=pandas.to_datetime(df.index)
ax = df['APPL'].plot()
# set monthly locator
ax.xaxis.set_major_locator(mdates.DayLocator(interval=3))
# set formatter
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
# set font and rotation for date tick labels
plt.gcf().autofmt_xdate()
Related
I have a dataframe that's in the format
date
class
01/06/2019 17:45
1
01/06/2019 20:23
2
03/06/2019 06:56
1
I'm trying to plot the changes in class against time using the code below
plt.plot_date(x, y, linestyle='solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%d-%m-%Y')
plt.gca().xaxis.set_major_formatter(date_format)
plt.tight_layout()
However the result is, well, a mess. There are numerous entries for each day separated by time but the graph is squashing down the x axis to only show the days
Is it possible to expand it so it uses day and time or even day and time in bins?
I'm currently working with some temperature data from a sensor that was active for about 4 months (from December 2018 to March 2019). I'm trying to plot the data; however, my time series currently goes from 350 to 430. How do I make the x-axis ticks start over at 0 once it reaches 365? Or, how can I add ticks that represent months starting at December and going to March?
Current graph:
Let's say you have your matplotlib.pyplot object, e.g. plt. We can use this to change the labels of the x-axis ticks:
xticks = plt.xticks()[0]
plt.xticks(xticks, (xticks % 365))
This question already has an answer here:
pyplot, why isn't the x-axis showing?
(1 answer)
Closed 3 years ago.
I am trying to plot a graph using matplotlib library.
This is my code:
df = pd.DataFrame()
df = milo_data2.loc[milo_data2['id'] == device]
plt.figure()
plt.title(device)
plt.ylabel('Counter')
plt.plot(df['timestamp'],df['counter'])
The graph looks like
The values on the x-axis are crowded and not readable.(The bold black line is the group of values overlapping each other) How do I reduce the number of values on the x-axis so that I can see some values on x-axis to get an estimate.
You can manually set the ticks to display. For instance, you can leave every tenth tick:
ticks = list(df['timestamp'])
plt.xticks([ticks[i] for i in range(len(ticks)) if i % 10 == 0], rotation='vertical')
For more information see documentation
I have two problems, but solving either of them neglects the need to solve the other.
I have a dataframe with temperature-values from e.g. 6 places in several (e.g. 3) heights. The heights are locked and are thus the indices of the dataframe. Example (does not match the figures):
place1 place2 place3 place4 place5 place6
1 12 10 9 13 10 4
2 18 15 13 14 8 8
3 21 9 16 15 11 12
I want to plot the temperatures for every place as a function of the height, HOWEVER!, I want the height (the indices) to be along the Y-AXIS instead of the x-axis. So instead of having the graphs running 'from left to right', I want the to run 'from down to up'.
If I just Plot a DataFrame it looks somewhat like this: The indices (heights) run along the X-axis and not the Y-axis.
- NOTE that values don't match table!!
ax.plot(data)
It does NOT work to transpose the array: it is still the values along the Y-axis that are flexible to locked X-values.
I want it to look like this with the variable temperature along the X-axis and the locked heights along the Y-axis. (PLEASE DON'T BOTHER the different dataset; the point is that the graphs are 'vertical' instead of horizontal):
ax.errorbar(temp, H, xerr=tempError, ecolor='gray')
The latter was made from plotting simple python lists and that work for me. However, my dataset is big so utilizing DataFrames would be nice AND, most importantly, I want to use the fill_betweenx to show the std instead of the errorbars currently presented as in the figure.
So I'm actually looking for an answer to any of these two questions:
How do i flip the plot from a DataFrame (i.e. not transpose the df, but make the axis swap)?
or, how do I utilize the fill_betweenx without using DataFrames?
Use pyplot.plot and structure the x's and y's the way you want.
from matplotlib import pyplot as plt
plt.plot(df.place1, df.index, df.place2, df.index, df.place3, df.index,
df.place4, df.index, df.place5, df.index, df.place6, df.index)
plt.show()
plt.close()
Or a little more general:
for name, series in df.iteritems():
plt.plot(series, df.index)
plt.show()
plt.close()
Here is suggestion for question no. 1
plt.plot(data, range(number_of_heights))
in your example number_of_heights is 3
This question already has answers here:
How to relabel axis ticks for a matplotlib heatmap
(3 answers)
Closed 1 year ago.
I am creating a heatmap in matplotlib where on the x and y axis is some parameter of a measurement and the color represents the value of the measurement. Matplotlib automatically gives the axes ticks based on the index of the value. For example if on the x axis I am measuring at 50 different values the ticks will be from 0 to 50. However the real value of this parameter is for example from -30 to 80 and I would like matplotlib to create the ticks based on this minimum and maximum.
I have tried using set_xticks but this requires the positions of the ticks as well as their labels. I am thinking that I should be able to just give matplotlib a min of -34 and max of 67 and have it create nice looking ticks placed at the proper positions but I haven't been able to find how.
After some digging in examples on the matplotlib website I found this option in imshow called extent in which you can replace the default zero-based coordinates with your own values for the min and max of both axes.
Wouldn't pyplot.xlim() or pyplot.figure.set_xlim() work in this case? Just say something like:
import matplotlib.pyplot as plt
plt.xlim(-30,80)
plt.ylim(0,100) #Or whatever
As far as I know the set_xticks function is too sophisticated for this. With that one you can specify what to put as your tick labels etc. For example if you want to associate a numerical series with a series of letters. For example:
x = [-8,-6,-4,-2,0,2,4,6,8]
labels = ['K2','K4','K6','K8','M0','M2','M4','M6','M8']
plt.xticks(x, labels)
Is one I used personally to translate integers into stellar spectral types (which is relevant, since I'm an astronomer ;p).
Hope this helps.
`