I've used Plotly for Python to inspect some coordinates corresponding with some text.
The text strings are sometimes so long that they go beyond the plot.
How could I fix this?
Code:
import plotly.express as px
fig = px.scatter(df, x='x', y='y', hover_data=['text'])
fig.show()
Related
I work with sports data and would like to visualize different shooting percentages at different places. I would like to make several text boxes, similar to in Microsoft word or powerpoint, and put them over each corresponding part of a picture of a goal to show the percentage at that place. I figured out how to get a picture into and R markdown but can't figure out how to make these text boxes and place them where I want.
You could add a chunk of R code to create a plot with your image as the background. Then you can add text to the plot, and it will appear over your image.
In the example below, I use an image in my working directory and the jpeg library:
```{r image_with_text, echo=FALSE}
library(jpeg)
#This image is in my working directory, but you may need to use a full path to your image:
my_image=readJPEG("beach-sunset-1483166052.jpg")
# Set up a blank plot
plot.new()
# get all the graphical parameters (as a named list).
#In this list, usr is a vector of the form c(x1, x2, y1, y2),
#giving the extremes of the user coordinates of the plotting region
gp <- par()
rasterImage(image=my_image,
xleft= gp$usr[1], ybottom=gp$usr[3], xright=gp$usr[2], ytop=gp$usr[4])
text(x=0.75, y=0.15, labels = "Wish You Were Here!", col="White")
```
The resulting image has the text in the bottom right corner (75% of the way along the x-axis, 15% of the way along the y-axis.)
Of course there are many ways to change the text, you can find out more about it here: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html
I have code below. Why don't the text labels appear? I want them on top of the each bar.
x=c(20,30,70,5,10,20)
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs")
text(aaa,labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
Your text statement only gives the argument aaa to position the text labels. So text is plotting your points at (1,5), (2,10), (3,20), (4,30) and (5,70) - all of which are off the graph and so do not display. You can get almost what you want by changing your text statement to:
text(aaa, table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
But that cuts off the highest label a little, so I recommend adjusting ylim on the plot.
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs",
ylim=c(0,max(table(x)))+0.1)
text(aaa,table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
I just started to use plotly for some interactive scatter plots in R and having a hard time on axis labels. Normally I designed my plots with ggplot2 and then using the ggplotly function to convert them, but this is sometimes very slow for any reason. So I want to create my plots directly in plotly...
I am trying now to change the axis title and want to add line breaks and later I also want to add subscript labels. But I am already failing at the newline in the title. Is there any trick?
library(plotly)
library(dplyr)
plot_ly(mtcars, x = wt, y = mpg, text = rownames(mtcars), mode = "text") %>%
layout(xaxis=list(title='text with\nlinebreak'))
In plotly, you can get linebreaks (and other text formatting) using html tags.
So piping
layout(xaxis=list(title='text with <br> linebreak'))
should work.
Hence, to get subscript labels use the <sub> tag. For example
CO<sub>2</sub>
will give you
CO2.
Note you have to use <br> or <br /> not <br/> (without a space)
How can I make a 3D plot without showing the axes?
When plotting a 3d plot, Matplotlib not only draws the x, y, and z axes, it draws light gray grids on the x-y, y-z, and x-z planes. I would like to draw a "free-floating" 3D plot, with none of these elements.
Stuff I've tried:
# Doesn't work; this hides the plot, not the axes
my_3d_axes.set_visible(False)
# Doesn't do anything. Also, there's no get_zaxis() function.
my_3d_axes.get_xaxis().set_visible(False)
my_3d_axes.get_yaxis().set_visible(False)
Ben Root provided a patch that fixes this for 1.0.1. It can be found as an attachment to the last email of this thread. To quote Ben:
Ok, looks like the hiding of the 3d axes was a feature added after the v1.0 release (but before I started working on mplot3d). This patch should enable the basic feature without interfering with existing functions. To hide the axes, you would have to set the private member "_axis3don" to False, like so:
ax = plt.gca(projection='3d')
ax._axis3don = False
If you do it this way, then you will get what you want now, and your code will still be compatible with mplot3d when you upgrade (although the preferred method would be to call set_axis_on() or set_axis_off()).
I hope that helps!
Ben Root
ax.set_axis_off()
Just to provide a concrete and direct example of what was mentioned at https://stackoverflow.com/a/7363931/895245
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
# Draw a circle on the x=0 'wall'
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="x")
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="z")
ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_zlim(-1.2, 1.2)
plt.savefig('main.png', format='png', bbox_inches='tight')
Output:
Without ax.set_axis_off() it would look like:
You will notice however that this produces an excessively large whitespace margin around the figure as it simply hides the axes but does not change the viewbox. I tried bbox_inches='tight' and it did not help as it does in 2D. How to solve that at: Remove white spaces in Axes3d (matplotlib)
Tested on matplotlib==3.2.2.
I'm trying to plot a log-log graph that shows logarithmically spaced grid lines at all of the ticks that you see along the bottom and left hand side of the plot. I've been able to show some gridlines by using matplotlib.pyplot.grid(True), but this is only showing grid lines for me at power of 10 intervals. So as an example, here is what I'm currently getting:
I'd really like something with grid lines looking more like this, where the gridlines aren't all evenly spaced:
How would I go about achieving this in Matplotlib?
Basically, you just need to put in the parameter which="both" in the grid command so that it becomes:
matplotlib.pyplot.grid(True, which="both")
Other options for which are 'minor' and 'major' which are the major ticks (which are shown in your graph) and the minor ticks which you are missing. If you want solid lines then you can use ls="-" as a parameter to grid() as well.
Here is an example for kicks:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0, 100, .5)
y = 2 * x**3
plt.loglog(x, y)
plt.grid(True, which="both", ls="-")
plt.show()
which generates:
More details on the Matplotlib Docs
As #Bryce says, in older version of matplotlib correct kwarg is which=majorminor. I think that solid lines with a lighter color can be better than the dotted lines.
plt.grid(True, which="majorminor", ls="-", color='0.65')
Note that in the latest version of matplotlib this argument is replaced by 'both'.
plt.grid(True, which="both", ls="-", color='0.65')