How to plot multiple variables in X axis in ggplot2? - ggplot2

I have an excel file containing the data shown here. I want to create a dot plot using ggplot2. I want to put mutations (shown in column) in X axis and Clade (shown in Rows like Alpha, Beta, Gamma etc.) in Y axis. I can't put multiple variables in X axis.

Related

How to widen the amount of my X axis data displayed?

My X label data is definietly too long to display every single position and still have transparent look of the chart.
fig, ax = plt.subplots()
ax.bar(timestamp, attribute_history)
fig.autofmt_xdate()
plt.show()
How to display for example every each 5 positions? My X label is taken directly from json and I'd like to avoid any operations on the data.
Also, is it possible to draw a straight line up through whole chart from each division line on the X axis?

Adding text using matplotlib

I am trying to build a graph using matplotlib, and I am having trouble placing descriptive text on the graph itself.
My y values range from .9 to 1.65, and x ranges from the dates 2001 to 2021 and are sourced from a datetime series.
Here are the basics of what I am working with:
fig, ax = plt.subplots(figsize=(10,7))
I know that I have to use ax.text() to place any text, but whenever I try to enter basically any values for the x and y coordinates of the text, the entire graph disappears when I re-run the cell. I have plotted the following line, but if I use the same coordinates in ax.text(), I get the output I just described. Why might this be happening?
plt.axhline(y=1.19, xmin=.032, xmax=.96)
By default, the y argument in the axhline method is in data coordinates, while the xmin and xmax arguments are in axis coordinates, with 0 corresponding to the far left of the plot, and 1 corresponding to the far right of the plot. See the axhline documentation for more information.
On the other hand, both the x and y arguments used in the text method are in data coordinates, so you position text relative to the data. However, you can change this to axis coordinates using the transform parameter. By setting this to ax.transAxes, you actually indicate that the x and y arguments should be interpreted as axis coordinates, again with 0 being the far left (or bottom) of the plot, and 1 being the far right (or top) of the plot. In this case, you would use ax.text as follows:
ax.text(x, y, 'text', transform=ax.transAxes)
Again, see the text documentation for more information.
However, it sounds like you might want to combine data and axis coordinates to place your text, because you want to reuse the arguments from axhline for your text. In this case, you need to create a transform object that interprets the x coordinate as axis coordinate, and the y coordinate as data coordinate. This is also possible by creating a blended transformation. For example:
import matplotlib.transforms as transforms
# create your ax object here
trans = transforms.blended_transform_factory(x_transform=ax.transAxes, y_transform=ax.transData)
ax.text(x, y, 'text', transform=trans)
See the Blended transformations section of the transformations tutorial for more information.
In short, you can refer to the following figure to compare the results of these various transformations:
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
fig, ax = plt.subplots()
ax.set_xlim(0, 2)
ax.set_ylim(0, 2)
# note that the line is plotted at y=1.5, but between x=1.6 and x=1.8
# because xmin/xmax are in axis coordinates
ax.axhline(1.5, xmin=.8, xmax=.9)
# x and y are in data coordinates
ax.text(0.5, 0.5, 'A')
# here, x and y are in axis coordinates
ax.text(0.5, 0.5, 'B', transform=ax.transAxes)
trans = transforms.blended_transform_factory(x_transform=ax.transAxes, y_transform=ax.transData)
# here, x is in axis coordinates, but y is in data coordinates
ax.text(0.5, 0.5, 'C', transform=trans)

how to change x axis in matplotlib

I want to change the scale of x axis in matplotlib in python. I am using following code.
df.iloc[:,5:9].plot(kind="density",subplots=True,layout=(2,2),sharex=False)
I want to have different X axis scale for all X axis. What I tried is following
fig=plt.figure(figsize=(15,15))
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax2=fig.add_subplot(2,2,3)
ax2=fig.add_subplot(2,2,4)
ax1.set_xticks(np.arrange(1,5000,500))
ax2.set_xticks(np.arrange(1,5000,500))
ax3.set_xticks(np.arrange(1,5000,500))
ax4.set_xticks(np.arrange(1,5000,500))
But, when I run this I get empty figure.
You need to operate on the axes in which the plots live, not in some newly created empty axes.
axes = df.plot(kind="density",subplots=True,layout=(2,2),sharex=False)
axes[0,0].set_xticks(...)

How can I set the number of ticks in Julia using Pyplot?

I am struggling to 'translate' the instructions I find for Python to the use of Pyplot in Julia. This must be a simple question, but do you know how to set the number of ticks in a plot in Julia using Pyplot?
If you have
x = [1,2,3,4,5]
y = [1,3,6,8,11]
you can
PyPlot.plot(x,y)
which draws the plot
and then do
PyPlot.xticks([1,3,5])
for tics at 1,3 and 5 on the x-axis
PyPlot.yticks([1,6,11])
for tics at 1,6 and 11 on the y-axis
Tic spacing
if you want fx 4 tics and want it evenly spaced and dont mind Floats, you can do
collect(linspace(x[1], x[end], 4).
If you need the tics to be integers and you want 4 tics, you can do
collect(x[1]:div(x[end],4):x[end])
Edit
Maybe this wont belong here but atleast you'll see it...
whenever you're looking for a method that's supposed to be in a module X you can find these methods by typing in the REPL X. + TAB key
to clarify, if you want to search a module for a method you suspect starts with an x, like xticts, in the REPL (terminal/shell) do
PyPlot.x
and press TAB twice and you'll see
julia> PyPlot.x
xkcd xlabel xlim xscale xticks
and if you're not sure exactly how the method works, fx its arguments, and there isnt any help available, you can call
methods(PyPlot.xticks)
to see every "version" that method has
Bonus
The module for all the standard methods, like maximum, vcat etc is Base
After some trying and searching, I found a way to do it. One can just set the number of bins that should be on each axis. Here is a minimal example:
using PyPlot
x = linspace(0, 10, 200)
y = sin(x)
fig, ax = subplots()
ax[:plot](x, y, "r-", linewidth=2, label="sine function", alpha=0.6)
ax[:legend](loc="upper center")
ax[:locator_params](axis ="y", nbins=4)
The last line specifies the number of bins that should be used on the y-axis. Leaving the argument axis unspecified will set that option for both axis at the same value.

Matplotlib plot colorbar label

I am trying to create a plot with a colorbar, with custom tick labels. When I try to create a label for a colorbar, it reverts to the original tick labels.
z = np.add(relzvals, shift_val)
fig=plt.figure()
plt.contourf(x,y,z,25)
ax = plt.colorbar()
ax.set_label('cbar_label',rotation=270)
ax.set_ticklabels(labels,update_ticks=True)
How can I get both customizations?
UPDATE: For context, the main program require translated values, i.e. relzvals = z + shift_val, and the matrix has to be translated back to z for plotting.
I checked the values of z before coming inside plt.contourf() but it contains the original values.
I have Python 2.7.11 and Matplotlib 1.3.1.