I am using plotly (python) in Dash to create a bar plot. I want to set the absolute height of the yaxis (not the full plot).
So each bar should have a maximum height of x pixels and the whole plot area including the tick labels can expand as necessary for the labels.
Is this possible?
figure explaining desired height
Related
I have a collection of images with a varying range of width and height as shown in the Table. What would be the best graphical illustration to show the relative widths and heights (width along the x-axis and height along the y-axis) along with the histograms plotted using Python?
I have a plotly graph that uses the Dash library to manipulate the x-values on the plot for simple comparison. When x values, in this case countries, is greater than 1, the legend is properly positioned outside of the graph. However, when there is only one country on the plot, the legend covers half of the plot.
I have tried: setting the legend x attribute to 3, x anchor to right, and changing the graph margin to add padding. I haven't found a solution that works.
Below is the section of the code that updates the plotly fig.
window_width = 1500
fig = px.bar(data_frame=dfi5.loc[(value_country)], width=(window_width / 4) * len(value_country))
fig.update_xaxes(showticklabels=True, title='')
fig.update_yaxes(showticklabels=True, title='Percent of Respondents')
fig.update_layout(autosize=False, title_text='Favorite K-Drama by Country', title_x=.46, title_font_size=20,
legend_title_text='Drama Title', margin_r=1, legend_xanchor='right', legend_x=3, legend_itemsizing='constant')
Update: I was able to find a workaround by reducing the legend font size, which reduces the area of the legend. I would be interested to learn if there is a way to explicitly set the location of the legend. The issue seems to occur when the legend area is wider than the plot area.
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))
I use the following to reduce width of bars in Panda:
for container in ax.containers:
plt.setp(container, width=.25)
However, on doing this, the labels on the x-axis remain at original position, as seen below. How can I move them to correspond to new bar width. In other words, is there a function to get the x coordinate of the center of each bar?
You may want to set the width during plot(), something like this:
df.plot(kind='bar', stacked=True, width=0.25, align='center')
In the document it doesn't show you can set the width, but in fact it will take it as **kwds
It will plot with the desired width with aligned x-axis labels.
My issue is that my x-axis tick labels are overlapping, as such I have a similar issue to the picture shown in this question:
matplotlib: how to prevent x-axis labels from overlapping each other
The distinction is that my labels are not rotated like they're in this picture. And I'm not using plt but instead:
ax=pylab.figure().add_subplot(111)
I am creating a bar chart and I can set the width of the bars. Can I similarly set the width of the xticklabels?
E.g so the first tick label appears on the x-axis between 0 and 0.5, the second appears between 0.5 and 1.0 etc...