i am unable to get the following plot to align properly along the x-axis. specifically, i want to plot a horizontal line representing the last value in the dataframe on top of a boxplot which describes the full sample. here is the code. currently i have commented out the line which would plot the boxplot
index = pd.date_range('1/1/2018', '2/1/2018')
data = pd.DataFrame({'a':np.random.randn(32)}, index=index)
fig, ax = plt.subplots(figsize=(6,3))
ax.hlines(data.iloc[-1],xmin=pd.RangeIndex(stop=len(list(data.columns)))+.15,xmax=pd.RangeIndex(stop=len(list(data.columns)))+.85,
**{'linewidth':1.5})
# ax.boxplot(data.values)
ax.set_xticks(pd.RangeIndex(stop=len(list(data.columns)))+0.5)
ax.set_xticklabels(list(data.columns), rotation=0)
ax.tick_params(axis='x',length=5, bottom=True)
here is the output from the above (so far so good)
if i uncomment the line from above, the code would produce this, which is misaligned:
any tips for how to get them to line up?
Apparently you have a very clear opinion about the boxplot to be positionned at x=0.5. But you forgot to tell the boxplot about that.
ax.boxplot(data.values, positions=[0.5])
Related
I am currently working on a barplot which looks like this:
In order to provide more clarity, I would like to add a vertical line that should point out the separation between the x values. I've drawn an example here:
In order to draw the diagram, I am using the plot function from pandas on the corresponding dataframe:
produced_items.plot(kind='bar', legend=True,title="Produced Items - Optimal solution",xlabel="Months",ylabel='Amount',rot=1,figsize=(15,5),width=0.8)
I hoped to find a parameter in matplotlib, that yields the desired behavior, but I didn't find anything, that could help.
Another solution that comes in my mind is to plot a vertical line between each x-value but i wonder if there is a built-in way to accomplish this?
Thanks in advance.
Let's try modifying the minor ticks:
from matplotlib.ticker import MultipleLocator
ax = df.plot.bar()
# set the minor ticks
ax.xaxis.set_minor_locator(MultipleLocator(0.5))
# play with the parameters to get your desired output
ax.tick_params(which='minor', length=15, direction='in')
Output:
I have a data file which consists of 131 columns and 4 rows. I am plotting it into python as follows
df = pd.read_csv('data.csv')
df.plot(figsize = (15,10))
Once it is plotted, all 131 legends are coming together like a huge tower over the line plots.
Please see the image here, which I have got :
Link to Image, I have clipped after v82 for better understanding
I have found some solutions on Stackoverflow (SO) to shift legend anywhere in the plot but I could not find any solution to break this legend tower into multiple small-small pieces and stack them one beside another.
Moreover, I want my plot something look like this
My desired plot :
Any help would be appreciable. Thank you.
You can specify the position of the legend in relative coordinates using loc and use ncol parameter to split the single legend column into multiple columns. To do so, you need an axis handle returned by the df.plot
df = pd.read_csv('data.csv')
ax = df.plot(figsize = (10,7))
ax.legend(loc=(1.01, 0.01), ncol=4)
plt.tight_layout()
I want to include a threshold and want to see which bars have crossed it.
First save the return value of the call to df.plot:
ax = df_6.plot(...)
Now you have a reference to the plot (ax is a matplotlib Axis object that represents the plot you just made). You can then add a horizontal line to the plot like this:
ax.axhline(y=5, color='red')
The Python code to create the plot used is below. This creates subplots based on "variable":
s=sb.relplot(x="timestamp",y="value",hue="variable",row="variable",
kind="line",facet_kws=dict(sharey=False),height=0.8, aspect=7,data=e)
plt.axvline(flip_timex)
s.fig.autofmt_xdate()
plt.show()
I need to add a vertical line (on a fixed date) on all the subplots generated.
plt.axvline(flip_timex) adds a vertical line only on one subplot.
You need to create the line in all generated plots.
grid = seaborn.relplot(...)
for ax in grid.axes.flat:
ax.axvline(...)
I am working on getting some graphs generated for 4 columns, with the COLUMN_NM being the main index.
The issue I am facing is the column names are showing along the bottom. This is problematic for 2 reasons, first being there could be dozens of these columns so the graph would look messy and could stretch too far to the right. Second being they are getting cut off (though I am sure that can be fixed)
I would prefer to have the column names listed vertically in the box where 'MAX_COL_LENGTH' current resides, and have the bars different colors per column instead.
Any ideas how I would adjust this or suggestions to make this better?
for col in ['DISTINCT_COUNT', 'MAX_COL_LENGTH', 'MIN_COL_LENGTH', 'NULL_COUNT']:
grid[['COLUMN_NM', col]].set_index('COLUMN_NM').plot.bar(title=col)
plt.show()
In this case you can plot points one by one and setup the label name for each point:
gs = gridspec.GridSpec(1,1)
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(gs[:, :])
data = [1,2,3,4,5]
label = ['l1','l2','l3','l4','l5']
for n,(p,l) in enumerate(zip(data,label)):
ax.bar(n,p,label=l)
ax.set_xticklabels([])
ax.legend()
This is the output for the code above: