I am trying to create two interactive plots where the first plot is simply a plot of x and y and the second plot is a subplot which plots dates (fulldate) on its x axis, which correspond to the integer values of x (x axis values) from the first plot.
This code almost does what I want. The only problem is that the dates are not linked to the integers, so when I use the zoom function on the graph, it zooms into the first plot and the subplot is linked and zooms also, but the dates stay stationary and therefore are completely inaccurate.
note that this is just a simplified version of my program. i will be rearranging the dates on the bottom display to got on the bottom.
The integers and the dates must be linked because in my actual program i will be using integers to keep count of the days in the time series.
import matplotlib.pyplot as plt
import seaborn as sns
x=[1,5,7,4,6]
y=[1,3,8,4,6]
fulldate=['01/01/2018','02/01/2018','03/01/2018','04/01/2018','05/01/2018']
with sns.axes_style("darkgrid"):
ax1=plt.subplot2grid((6,1),(0,0),rowspan=3,colspan=1)
ax2=plt.subplot2grid((6,1),(4,0),rowspan=1,colspan=1,sharex=ax1)
ax2v = ax2.twiny()
ax1.plot(x,y)
ax2v.fill_between(fulldate,'Dates')
for label in ax2v.xaxis.get_ticklabels():
label.set_rotation(60)
Related
I've got a pandas dataframe with a bunch of values in and I want to plot each axis against each axis to get plots of every column against one another. Furthermore, I'm having an issue of the values of my y axis being so condensed that's it's unreadable. I've tried changing the height but have no clue how to "clean up" this axis.
Here is my plotting code:
import seaborn as sns
grid = sns.pairplot(df_merge, dropna = True, height=1.5)
Then here is the graph that has been plotted.
I have a very simple data frame but I could not plot a line using a row and a column. Here is an image, I would like to plot a "line" that connects them.
enter image description here
I tried to plot it but x-axis disappeared. And I would like to swap those axes. I could not find an easy way to plot this simple thing.
Try:
import matplotlib.pyplot as plt
# Categories will be x axis, sexonds will be y
plt.plot(data["Categories"], data["Seconds"])
plt.show()
Matplotlib generates the axis dynamically, so if you want the labels of the x-axis to appear you'll have to increase the size of your plot.
I have 2 subplots in matplotlib in Python. They are stacked on top of each other.
I want to have gridlines on each plot, which I have done successfully. But each plot has a different x axis and, therefore, the vertical grid lines of the top plot are not aligned with those of the bottom plot.
I would like the grid lines of the top plot to be in the same position on the x axis as they are on the bottom plot i.e. the vertical grid lines in both plots should be aligned.
I imaging that I can tell my grid lines exactly where to be, and so I could achieve my goal by adjusting the lines until they match as well as possible.
I just hoped that there might be some easier way that would just allow me to align the gridlines on both plots.
Edit:
I don't think the shared axis stuff is quite what I want.
My top and bottom plot have very different scales, so when I share the axes, it shifts the scaling too. For example, say my top plot has data that runs from 0-100 on the x axis and on the bottom plot the data runs from 0-50. When I share the axis, the top plot only shows data from 0-50, which I don't want it to.
I want it to show from 0-100 as it did before, but just want it to share the axis and gridlines from the other plot.
You could use LinearLocator:
from matplotlib.ticker import LinearLocator
Then on each of your x-axis or only on one of them call:
N = 6 # Set number of gridlines you want to have in each graph
ax1.xaxis.set_major_locator(LinearLocator(N))
ax2.xaxis.set_major_locator(LinearLocator(N))
Or get the number of ticks from your source axis and set it on target axis:
N = source_ax.xaxis.get_major_ticks()
target_ax.xaxis.set_major_locator(LinearLocator(N))
Using the AXIS notation for matplotlib has allowed me to manually plot a grid of 2x2 or 3x3 or whatever size grid (if I know what size grid I want beforehand.)
However, how do you determine what size grid is needed automatically. Like what if you don't know how many unique values are in a column that you want to graph?
I am thinking there must be a way of doing this in a loop and figuring out based on the number of unique values in the column this is how big the graph needs to be.
Example
When I plot this for some reason it doesn't show month_name on the x axis (as in Jan, Feb, Marc etc)
avg_all_account.plot(legend=False,subplots=True,x='month_date',figsize=(10,20))
plt.xlabel('month')
plt.ylabel('number of proposals')
Yet when I plot subplots on a figure and specify x axis paremeter x='month_name' The month name appears on the plot here:
f = plt.figure()
f.set_figheight(8)
f.set_figwidth(8)
f.sharex=True
f.sharey=True
#graph1 = f.add_subplot(2,2,1)
avg_all_account.ix[0:,['month_date','number_open_proposals_all']].plot(ax=f.add_subplot(331),legend=False,subplots=True,x='month_date',y='number_open_proposals_all',title='open proposals')
plt.xlabel('month')
plt.ylabel('number of proposals')
Thus because the subplot method worked and showed the month_name on the x axis, and my x and y axis labels: I wanted to know how would I work out how many subplots I would need without first calculating it, then writing out each line and hard coding the subplot position?
So I have a graph that runs on an order of magnitude 10000 time steps, and thus I have a lot of data points and the xticks are spaced pretty far apart, which is cool, but I would like to to show on the xaxis the point at which the data is being plotted. In this case the xtick I want to show is 271. So is there a way to just "insert" 271 tick onto the x axis given that I already know what tick I want to display?
If it's not important that the ticks update when panning/zomming (i.e. if the plot is not meant for interactive use), then you can manually set the tick locations with the axes.set_xticks() method. In order to append one location (e.g. 271), you can first get the current tick locations with axes.get_xticks(), and then append 271 to this array.
A short example:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange(300))
# Get current tick locations and append 271 to this array
x_ticks = np.append(ax.get_xticks(), 271)
# Set xtick locations to the values of the array `x_ticks`
ax.set_xticks(x_ticks)
plt.show()
This produces
As you can see from the image, a tick has been added for x=271.